1 ##############################################################################
2 # Copyright (c) 2015 Huawei Technologies Co.,Ltd and others.
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
12 from vstf.common import constants
13 from vstf.common import message
16 class UdpServer(object):
19 super(UdpServer, self).__init__()
21 os.unlink(constants.sockaddr)
23 if os.path.exists(constants.sockaddr):
24 raise Exception("socket not found %s" % constants.sockaddr)
25 self.conn = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
27 def listen(self, backlog=5):
28 self.conn.listen(backlog)
31 return self.conn.accept()
33 def bind(self, addr=constants.sockaddr):
34 return self.conn.bind(addr)
36 # def send(self, data, addr):
37 # return message.sendto(self.conn.sendto, data, addr)
39 # def recv(self, size=constants.buff_size):
40 # return message.recv(self.conn.recvfrom)
46 class UdpClient(object):
49 super(UdpClient, self).__init__()
50 if not os.path.exists(constants.sockaddr):
51 raise Exception("socket not found %s" % constants.sockaddr)
52 self.conn = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
54 def connect(self, addr=constants.sockaddr):
55 return self.conn.connect(addr)
58 message.send(self.conn.send, data)
61 return message.recv(self.conn.recv)