decidedly... sfa_rspec_empty needs the sfi/pg dir to have been created beforehand
[tests.git] / system / tcptest.py
1 #!/usr/bin/env python
2
3 # Thierry Parmentelat <thierry.parmentelat@inria.fr>
4 # Copyright (C) 2010 INRIA 
5 #
6 import sys
7 import time
8 from optparse import OptionParser    
9 import socket
10 import SocketServer
11
12 def myprint(message):
13     now=time.strftime("%H:%M:%S", time.localtime())
14     print "*",now,'--',message
15
16 class EchoRequestHandler(SocketServer.StreamRequestHandler):
17     def handle(self):
18         line = self.rfile.readline()
19         self.wfile.write(line)
20
21 class UppercaseRequestHandler(SocketServer.StreamRequestHandler):
22     def handle(self):
23         line = self.rfile.readline()
24         self.wfile.write(line.upper())
25
26 class Server:
27
28     def main(self):
29         import threading
30
31         parser=OptionParser()
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",
37                           default="0")
38         
39         (options, args) = parser.parse_args()
40         if len(args) != 0:
41             parser.print_help()
42             sys.exit(1)
43
44         server = SocketServer.TCPServer((options.address, options.port),
45                                         UppercaseRequestHandler)
46
47         try:
48             if options.timeout:
49                 t = threading.Thread(target=server.serve_forever)
50                 t.setDaemon(True) # don't hang on exit
51                 t.start()
52                 time.sleep(options.timeout)
53                 sys.exit(0)
54             else:
55                 server.serve_forever()        
56         except KeyboardInterrupt:
57             print 'Bailing out'
58             sys.exit(1)
59
60 class Client:
61     def main(self):
62         from optparse import OptionParser    
63
64         parser=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")
73         
74         (options, args) = parser.parse_args()
75         if len(args) != 0:
76             parser.print_help()
77             sys.exit(1)
78
79         result=True
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))
83             mout=i*'ping ' + '\n'
84             min=mout.upper()
85             if s.send(mout) != len(mout):
86                 myprint("cannot send %s"%mout.strip())
87                 result=False
88                 break
89             line=s.recv(len(min))
90             if line is not line:
91                 myprint("unexpected reception\ngot:%s\nexpected: %s",line,min)
92                 result=False
93             else:
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")
99             s.close()
100         myprint("Done")
101         exit_return=0
102         if not result: exit_return=1
103         sys.exit(exit_return)
104
105 if __name__ == '__main__':
106     for argv in sys.argv[1:]:
107         if argv.find("client") >= 0:
108             sys.argv.remove(argv)
109             Client().main()
110         elif argv.find("server") >= 0:
111             sys.argv.remove(argv)
112             Server().main()
113     print 'you must specify either --client or --server'
114     sys.exit(1)