3 # Thierry Parmentelat <thierry.parmentelat@inria.fr>
4 # Copyright (C) 2010 INRIA
8 from optparse import OptionParser
13 now=time.strftime("%H:%M:%S", time.localtime())
14 print "*",now,'--',message
16 class EchoRequestHandler(SocketServer.StreamRequestHandler):
18 line = self.rfile.readline()
19 self.wfile.write(line)
21 class UppercaseRequestHandler(SocketServer.StreamRequestHandler):
23 line = self.rfile.readline()
24 self.wfile.write(line.upper())
32 parser.add_option("-p","--port", action="store", dest="port", type="int",
33 default=10000, help="port number")
34 parser.add_option("-a","--address", action="store", dest="address",
35 default=socket.gethostname(), help="address")
36 parser.add_option("-t","--timeout", action="store", dest="timeout", type="int",
39 (options, args) = parser.parse_args()
44 server = SocketServer.TCPServer((options.address, options.port),
45 UppercaseRequestHandler)
49 t = threading.Thread(target=server.serve_forever)
50 t.setDaemon(True) # don't hang on exit
52 time.sleep(options.timeout)
55 server.serve_forever()
56 except KeyboardInterrupt:
62 from optparse import OptionParser
65 parser.add_option("-p","--port", action="store", dest="port", type="int",
66 default=10000, help="port number")
67 parser.add_option("-a","--address", action="store", dest="address",
68 default=socket.gethostname(), help="address")
69 parser.add_option("-s","--sleep", action="store", dest="sleep", type="int",
70 default=1, help="sleep seconds")
71 parser.add_option("-l","--loops", action="store", dest="loops", type="int",
72 default=1, help="iteration loops")
74 (options, args) = parser.parse_args()
80 for i in range(1,options.loops+1):
81 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
82 s.connect((options.address , options.port))
85 if s.send(mout) != len(mout):
86 myprint("cannot send %s"%mout.strip())
91 myprint("unexpected reception\ngot:%s\nexpected: %s",line,min)
94 myprint("OK:%s"%mout.strip())
95 # leave the connection open, but the last one (so 1 iter returns fast)
96 if i != options.loops:
97 time.sleep(options.sleep)
98 myprint("disconnecting")
102 if not result: exit_return=1
103 sys.exit(exit_return)
105 if __name__ == '__main__':
106 for argv in sys.argv[1:]:
107 if argv.find("client") >= 0:
108 sys.argv.remove(argv)
110 elif argv.find("server") >= 0:
111 sys.argv.remove(argv)
113 print 'you must specify either --client or --server'