X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=system%2Ftcptest.py;h=abdc6fb3acf480bd67ca60fb98c414163e223c66;hb=457d31694f7b0f60e2a6fea230e9a3572b9d2b78;hp=bff3c668ce151143a8a2d968d6f05c2dbf063304;hpb=9d9c35913d1312a42ef63fc665f44aabc80cf001;p=tests.git diff --git a/system/tcptest.py b/system/tcptest.py index bff3c66..abdc6fb 100755 --- a/system/tcptest.py +++ b/system/tcptest.py @@ -3,6 +3,14 @@ # Thierry Parmentelat # Copyright (C) 2010 INRIA # + +# this is a small and simple standalone utility +# designed to run in slice-space +# we keep this in python2 for now until python3 +# can be taken for granted in sliceimage + +from __future__ import print_function + import sys import time import subprocess @@ -12,8 +20,8 @@ import threading from optparse import OptionParser def myprint(message, id='client'): - now=time.strftime("%H:%M:%S", time.localtime()) - print "*",now,'(%s)' % id, '--',message + now = time.strftime("%H:%M:%S", time.localtime()) + print("* {now} ({id}) -- {message}".format(**locals())) sys.stdout.flush() def show_network_status(id): @@ -50,6 +58,7 @@ class Server: parser.print_help() sys.exit(1) + myprint("==================== tcptest.py server", id='server') show_network_status(id='server') server = SocketServer.TCPServer((options.address, options.port), UppercaseRequestHandler) @@ -63,7 +72,7 @@ class Server: else: server.serve_forever() except KeyboardInterrupt as e: - print 'Bailing out on keyboard interrupt' + print('Bailing out on keyboard interrupt') sys.exit(1) class Ready: @@ -82,13 +91,21 @@ class Ready: default=socket.gethostname(), help="address") (options, args) = parser.parse_args() - s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - try: - s.bind((options.address, options.port)) - sys.exit(0) - except Exception as e: - print e - sys.exit(1) + myprint("==================== tcptest.py ready", id='ready') + def can_bind (): + s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + try: + s.bind((options.address, options.port)) + return True + except Exception as e: + print(e) + return False + + def eth0_has_ipv4(): + command = "ip address show eth0 | grep -q ' inet '" + return subprocess.check_call(command, shell=True) == 0 + + sys.exit(0 if can_bind() and eth0_has_ipv4() else 1) class Client: """ @@ -110,6 +127,7 @@ class Client: parser.print_help() sys.exit(1) + myprint("==================== tcptest.py client", id='client') result=True for i in range(1,options.loops+1): s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) @@ -117,15 +135,15 @@ class Client: mout=i*'ping ' + '\n' min=mout.upper() if s.send(mout) != len(mout): - myprint("cannot send %s"%mout.strip()) + myprint("cannot send {}".format(mout.strip())) result=False break line=s.recv(len(min)) if line is not line: - myprint("unexpected reception\ngot:%s\nexpected: %s",line,min) - result=False + myprint("unexpected reception\ngot:{}\nexpected: {}".format(line, min)) + result = False else: - myprint("OK:%s"%mout.strip()) + myprint("OK:{}".format(mout.strip())) # leave the connection open, but the last one (so 1 iter returns fast) if i != options.loops: time.sleep(options.sleep) @@ -148,5 +166,5 @@ if __name__ == '__main__': elif arg.find("ready") >= 0: sys.argv.remove(arg) Ready().main() - print 'you must specify either --client or --server' + print('you must specify either --client or --server') sys.exit(1)