3 # Thierry Parmentelat <thierry.parmentelat@inria.fr>
4 # Copyright (C) 2010 INRIA
7 from __future__ import print_function
15 from optparse import OptionParser
17 def myprint(message, id='client'):
18 now = time.strftime("%H:%M:%S", time.localtime())
19 print("* {now} ({id}) -- {message}".format(**locals()))
22 def show_network_status(id):
23 myprint("ip address show", id=id)
24 subprocess.call(['ip', 'address', 'show'])
25 myprint("ip route show", id=id)
26 subprocess.call(['ip', 'route', 'show'])
28 class EchoRequestHandler(SocketServer.StreamRequestHandler):
30 line = self.rfile.readline()
31 self.wfile.write(line)
33 class UppercaseRequestHandler(SocketServer.StreamRequestHandler):
35 line = self.rfile.readline()
36 self.wfile.write(line.upper())
40 A TCP server, running for some finite amount of time
43 parser = OptionParser()
44 parser.add_option("-p", "--port", action="store", dest="port", type="int",
45 default=10000, help="port number")
46 parser.add_option("-a", "--address", action="store", dest="address",
47 default=socket.gethostname(), help="address")
48 parser.add_option("-t", "--timeout", action="store", dest="timeout", type="int",
50 (options, args) = parser.parse_args()
56 myprint("==================== tcptest.py server", id='server')
57 show_network_status(id='server')
58 server = SocketServer.TCPServer((options.address, options.port),
59 UppercaseRequestHandler)
62 t = threading.Thread(target=server.serve_forever)
63 t.setDaemon(True) # don't hang on exit
65 time.sleep(options.timeout)
68 server.serve_forever()
69 except KeyboardInterrupt as e:
70 print('Bailing out on keyboard interrupt')
75 A utility that does exit(0) iff network as perceived
76 from the sliver is ready. Designed to be run before Server,
77 so one can wait for the right conditions.
80 parser = OptionParser()
81 # by default use another port so we don't run into
82 # the SO_LINGER kind of trouble
83 parser.add_option("-p", "--port", action="store", dest="port", type="int",
84 default=9999, help="port number")
85 parser.add_option("-a", "--address", action="store", dest="address",
86 default=socket.gethostname(), help="address")
87 (options, args) = parser.parse_args()
89 myprint("==================== tcptest.py ready", id='ready')
91 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
93 s.bind((options.address, options.port))
95 except Exception as e:
100 command = "ip address show eth0 | grep -q ' inet '"
101 return subprocess.check_call(command, shell=True) == 0
103 sys.exit(0 if can_bind() and eth0_has_ipv4() else 1)
107 Runs a client against a Server instance
110 parser = OptionParser()
111 parser.add_option("-p","--port", action="store", dest="port", type="int",
112 default=10000, help="port number")
113 parser.add_option("-a","--address", action="store", dest="address",
114 default=socket.gethostname(), help="address")
115 parser.add_option("-s","--sleep", action="store", dest="sleep", type="int",
116 default=1, help="sleep seconds")
117 parser.add_option("-l","--loops", action="store", dest="loops", type="int",
118 default=1, help="iteration loops")
120 (options, args) = parser.parse_args()
125 myprint("==================== tcptest.py client", id='client')
127 for i in range(1,options.loops+1):
128 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
129 s.connect((options.address, options.port))
130 mout=i*'ping ' + '\n'
132 if s.send(mout) != len(mout):
133 myprint("cannot send {}".format(mout.strip()))
136 line=s.recv(len(min))
138 myprint("unexpected reception\ngot:{}\nexpected: {}".format(line, min))
141 myprint("OK:{}".format(mout.strip()))
142 # leave the connection open, but the last one (so 1 iter returns fast)
143 if i != options.loops:
144 time.sleep(options.sleep)
145 myprint("disconnecting")
151 sys.exit(exit_return)
153 if __name__ == '__main__':
154 for arg in sys.argv[1:]:
155 if arg.find("client") >= 0:
158 elif arg.find("server") >= 0:
161 elif arg.find("ready") >= 0:
164 print('you must specify either --client or --server')