Changeset 186
- Timestamp:
- 03/04/06 22:58:15 (2 years ago)
- Files:
-
- feedtree/trunk/tools/networktest/networktest.py (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
feedtree/trunk/tools/networktest/networktest.py
r185 r186 4 4 5 5 TEST_TEXT = 'test' 6 TEST_HOST = 'feedtree.net' 7 DEFAULT_PORT = 29690 6 TEST_HOST = 'pd05.cs.rice.edu' 7 8 CLIENT_PORT = 29690 9 SERVER_TCP_PORT = 29680 10 SERVER_UDP_PORT = 29681 8 11 9 12 class NetworkTest: 10 def __init__(self, host=TEST_HOST, port=DEFAULT_PORT, localhost=None): 11 self.server = host 12 self.port = port 13 if not localhost: 14 localhost = socket.gethostname() 13 def __init__(self, localhost=None, testhost=TEST_HOST): 14 self.server = testhost 15 if localhost: 16 self.localhost = localhost 17 else: 18 self.localhost = socket.gethostname() 19 self.ipaddr = socket.gethostbyname(self.localhost) 15 20 16 (self.hostname, self.aliases, self.ipaddr) \ 17 = socket.gethostbyaddr(localhost) 21 # try: 22 # (self.hostname, self.aliases, self.ipaddr) \ 23 # = socket.gethostbyaddr(localhost) 24 # except: 25 # (self.hostname, self.aliases, self.ipaddr) \ 26 # = (None, None, None) 18 27 19 28 def test_tcp_out(self): … … 21 30 s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) 22 31 s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1) 23 print "test_tcp_out: connecting to %s:%d" % (self.server, self.port)24 s.connect((self.server, self.port))32 print "test_tcp_out: connecting to %s:%d" % (self.server, SERVER_TCP_PORT) 33 s.connect((self.server, SERVER_TCP_PORT)) 25 34 s.send(TEST_TEXT) 26 35 data = s.recv(1024) … … 32 41 s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) 33 42 s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1) 34 s.bind((socket.gethostname(), self.port)) 43 print "test_tcp_in: listening on %s:%d" % (self.ipaddr, CLIENT_PORT) 44 s.bind((socket.gethostname(), CLIENT_PORT)) 45 s.settimeout(15) 46 print "test_tcp_in: waiting...(15 sec max)" 35 47 s.listen(1) 36 48 37 49 (cs, addr) = s.accept() 50 print "test_tcp_in: sending to %s: %s " % (addr, TEST_TEXT) 38 51 cs.send(TEST_TEXT) 39 52 data = cs.recv(1024) 53 print "test_tcp_in: response from %s: %s " % (addr, data) 40 54 cs.close() 41 55 … … 46 60 s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) 47 61 s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1) 48 s.bind((socket.gethostname(), self.port)) 62 print "test_udp_in: listening on %s:%d" % (socket.gethostname(), CLIENT_PORT) 63 s.bind((socket.gethostname(), CLIENT_PORT)) 49 64 50 print "test_udp_ out: connecting to %s:%d" % (self.server, self.port)51 cs.sendto((self.server, self.port), TEST_TEXT)52 (data, addr) = cs.recvfrom(1024)53 print "test_udp_ out: response: " + data65 print "test_udp_in: connecting to %s:%d" % (self.server, SERVER_UDP_PORT) 66 s.sendto(TEST_TEXT, (self.server, SERVER_UDP_PORT)) 67 (data, addr) = s.recvfrom(1024) 68 print "test_udp_in: response from %s: %s " % (addr, data) 54 69 55 70 s.close() … … 57 72 if __name__ == '__main__': 58 73 if len(sys.argv) > 1: 59 TEST_HOST= sys.argv[1]60 if len(sys.argv) > 2:61 DEFAULT_PORT = (sys.argv[2])74 localhost = sys.argv[1] 75 else: 76 localhost = None 62 77 63 nt = NetworkTest(TEST_HOST, DEFAULT_PORT) 64 nt.test_tcp_out() 65 nt.test_tcp_in() 66 nt.test_udp_in() 78 test = 'startup' 79 try: 80 nt = NetworkTest(localhost) 81 print "using IP address: %s (%s)" % (nt.ipaddr, nt.localhost) 82 print "(if this is the wrong IP for your Internet connection, specify the correct address as the first command-line argument)" 83 test = 'tcp_out' 84 nt.test_tcp_out() 85 test = 'tcp_in' 86 nt.test_tcp_in() 87 test = 'udp_in' 88 nt.test_udp_in() 89 print "all tests passed" 90 except Exception, e: 91 print "test %s failed (%s)" % (test, str(e)) 67 92
