From: Thierry Parmentelat Date: Wed, 13 Feb 2008 18:42:49 +0000 (+0000) Subject: first draft X-Git-Tag: tests-4.2-4~247 X-Git-Url: http://git.onelab.eu/?a=commitdiff_plain;h=4f6a825ba807adbae8dbe3d520e3bc0e76d24dde;p=tests.git first draft --- diff --git a/system/tcptest.py b/system/tcptest.py new file mode 100755 index 0000000..975bc16 --- /dev/null +++ b/system/tcptest.py @@ -0,0 +1,111 @@ +#!/usr/bin/env python + +import sys +import time +from optparse import OptionParser +import socket +import SocketServer + +def myprint(message): + now=time.strftime("%H:%M:%S", time.localtime()) + print "*",now,'--',message + +class EchoRequestHandler(SocketServer.StreamRequestHandler): + def handle(self): + line = self.rfile.readline() + self.wfile.write(line) + +class UppercaseRequestHandler(SocketServer.StreamRequestHandler): + def handle(self): + line = self.rfile.readline() + self.wfile.write(line.upper()) + +class Server: + + def main(self): + import threading + + parser=OptionParser() + parser.add_option("-p","--port", action="store", dest="port", type="int", + default=10000, help="port number") + parser.add_option("-a","--address", action="store", dest="address", + default=socket.gethostname(), help="address") + parser.add_option("-t","--timeout", action="store", dest="timeout", type="int", + default="0") + + (options, args) = parser.parse_args() + if len(args) != 0: + parser.print_help() + sys.exit(1) + + server = SocketServer.TCPServer((options.address, options.port), + UppercaseRequestHandler) + + try: + if options.timeout: + t = threading.Thread(target=server.serve_forever) + t.setDaemon(True) # don't hang on exit + t.start() + time.sleep(options.timeout) + sys.exit(0) + else: + server.serve_forever() + except KeyboardInterrupt: + print 'Bailing out' + sys.exit(1) + +class Client: + def main(self): + from optparse import OptionParser + + parser=OptionParser() + parser.add_option("-p","--port", action="store", dest="port", type="int", + default=10000, help="port number") + parser.add_option("-a","--address", action="store", dest="address", + default=socket.gethostname(), help="address") + parser.add_option("-s","--sleep", action="store", dest="sleep", type="int", + default=1, help="sleep seconds") + parser.add_option("-l","--loops", action="store", dest="loops", type="int", + default=1, help="iteration loops") + + (options, args) = parser.parse_args() + if len(args) != 0: + parser.print_help() + sys.exit(1) + + result=True + for i in range(1,options.loops+1): + s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + s.connect((options.address , options.port)) + mout=i*'ping ' + '\n' + min=mout.upper() + if s.send(mout) != len(mout): + myprint("cannot send %s"%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 + else: + myprint("OK:%s"%mout.strip()) + # leave the connection open, but the last one (so 1 iter returns fast) + if i != options.loops: + time.sleep(options.sleep) + myprint("disconnecting") + s.close() + myprint("Done") + exit_return=0 + if not result: exit_return=1 + sys.exit(exit_return) + +if __name__ == '__main__': + for argv in sys.argv[1:]: + if argv.find("client") >= 0: + sys.argv.remove(argv) + Client().main() + elif argv.find("server") >= 0: + sys.argv.remove(argv) + Server().main() + print 'you must specify either --client or --server' + sys.exit(1)