3 # Thierry Parmentelat <thierry.parmentelat@inria.fr>
4 # Copyright (C) 2010 INRIA
7 # this is a small and simple standalone utility
8 # designed to run in slice-space
9 # we keep this in python2 for now until python3
10 # can be taken for granted in sliceimage
12 from __future__ import print_function
20 from optparse import OptionParser
22 def myprint(message, id='client'):
23 now = time.strftime("%H:%M:%S", time.localtime())
24 print("* {now} ({id}) -- {message}".format(**locals()))
27 def show_network_status(id):
28 myprint("ip address show", id=id)
29 subprocess.call(['ip', 'address', 'show'])
30 myprint("ip route show", id=id)
31 subprocess.call(['ip', 'route', 'show'])
33 class EchoRequestHandler(SocketServer.StreamRequestHandler):
35 line = self.rfile.readline()
36 self.wfile.write(line)
38 class UppercaseRequestHandler(SocketServer.StreamRequestHandler):
40 line = self.rfile.readline()
41 self.wfile.write(line.upper())
45 A TCP server, running for some finite amount of time
48 parser = OptionParser()
49 parser.add_option("-p", "--port", action="store", dest="port", type="int",
50 default=10000, help="port number")
51 parser.add_option("-a", "--address", action="store", dest="address",
52 default=socket.gethostname(), help="address")
53 parser.add_option("-t", "--timeout", action="store", dest="timeout", type="int",
55 (options, args) = parser.parse_args()
61 myprint("==================== tcptest.py server", id='server')
62 show_network_status(id='server')
63 server = SocketServer.TCPServer((options.address, options.port),
64 UppercaseRequestHandler)
67 t = threading.Thread(target=server.serve_forever)
68 t.setDaemon(True) # don't hang on exit
70 time.sleep(options.timeout)
73 server.serve_forever()
74 except KeyboardInterrupt as e:
75 print('Bailing out on keyboard interrupt')
80 A utility that does exit(0) iff network as perceived
81 from the sliver is ready. Designed to be run before Server,
82 so one can wait for the right conditions.
85 parser = OptionParser()
86 # by default use another port so we don't run into
87 # the SO_LINGER kind of trouble
88 parser.add_option("-p", "--port", action="store", dest="port", type="int",
89 default=9999, help="port number")
90 parser.add_option("-a", "--address", action="store", dest="address",
91 default=socket.gethostname(), help="address")
92 (options, args) = parser.parse_args()
94 myprint("==================== tcptest.py ready", id='ready')
96 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
98 s.bind((options.address, options.port))
100 except Exception as e:
105 command = "ip address show eth0 | grep -q ' inet '"
106 return subprocess.check_call(command, shell=True) == 0
108 sys.exit(0 if can_bind() and eth0_has_ipv4() else 1)
112 Runs a client against a Server instance
115 parser = OptionParser()
116 parser.add_option("-p","--port", action="store", dest="port", type="int",
117 default=10000, help="port number")
118 parser.add_option("-a","--address", action="store", dest="address",
119 default=socket.gethostname(), help="address")
120 parser.add_option("-s","--sleep", action="store", dest="sleep", type="int",
121 default=1, help="sleep seconds")
122 parser.add_option("-l","--loops", action="store", dest="loops", type="int",
123 default=1, help="iteration loops")
125 (options, args) = parser.parse_args()
130 myprint("==================== tcptest.py client", id='client')
132 for i in range(1,options.loops+1):
133 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
134 s.connect((options.address, options.port))
135 mout=i*'ping ' + '\n'
137 if s.send(mout) != len(mout):
138 myprint("cannot send {}".format(mout.strip()))
141 line=s.recv(len(min))
143 myprint("unexpected reception\ngot:{}\nexpected: {}".format(line, min))
146 myprint("OK:{}".format(mout.strip()))
147 # leave the connection open, but the last one (so 1 iter returns fast)
148 if i != options.loops:
149 time.sleep(options.sleep)
150 myprint("disconnecting")
156 sys.exit(exit_return)
158 if __name__ == '__main__':
159 for arg in sys.argv[1:]:
160 if arg.find("client") >= 0:
163 elif arg.find("server") >= 0:
166 elif arg.find("ready") >= 0:
169 print('you must specify either --client or --server')