9 def runTCP(host, port, buffer_size):
11 BUFFER_SIZE = buffer_size
12 server = SocketServer.TCPServer((host, port), Handler)
13 server.serve_forever()
17 print "(%d bytes)" % sys.getsizeof(data)
19 class Handler(SocketServer.StreamRequestHandler):
23 data = self.request.recv(BUFFER_SIZE)
31 clientsock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
32 clientsock.bind((host, port))
34 recv_msg, addr = clientsock.recvfrom(buffer_size)
38 if __name__ ==
"__main__":
39 parser = argparse.ArgumentParser(description=
'Simple socket server')
40 parser.add_argument(
"--port", default=8080, type=int)
41 parser.add_argument(
"--buffer-size", default=1024, type=int)
42 parser.add_argument(
"--udp", action=
"store_true")
43 args = parser.parse_args()
46 print 'listening UDP', (HOSTNAME, args.port),
"buffer size is", args.buffer_size
47 runUDP(HOSTNAME, args.port, args.buffer_size)
49 print 'listening TCP', (HOSTNAME, args.port),
"buffer size is", args.buffer_size
50 runTCP(HOSTNAME, args.port, args.buffer_size)
def runTCP(host, port, buffer_size)
def runUDP(host, port, buffer_size)