Changeset 186

Show
Ignore:
Timestamp:
03/04/06 22:58:15 (2 years ago)
Author:
dsandler
Message:

mostly works

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • feedtree/trunk/tools/networktest/networktest.py

    r185 r186  
    44 
    55TEST_TEXT = 'test' 
    6 TEST_HOST = 'feedtree.net' 
    7 DEFAULT_PORT = 29690 
     6TEST_HOST = 'pd05.cs.rice.edu' 
     7 
     8CLIENT_PORT = 29690 
     9SERVER_TCP_PORT = 29680 
     10SERVER_UDP_PORT = 29681 
    811 
    912class 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) 
    1520 
    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) 
    1827 
    1928    def test_tcp_out(self): 
     
    2130        s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) 
    2231        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)) 
    2534        s.send(TEST_TEXT) 
    2635        data = s.recv(1024) 
     
    3241        s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) 
    3342        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)" 
    3547        s.listen(1) 
    3648 
    3749        (cs, addr) = s.accept() 
     50        print "test_tcp_in: sending to %s: %s " % (addr, TEST_TEXT) 
    3851        cs.send(TEST_TEXT) 
    3952        data = cs.recv(1024) 
     53        print "test_tcp_in: response from %s: %s " % (addr, data) 
    4054        cs.close() 
    4155 
     
    4660        s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) 
    4761        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)) 
    4964 
    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: " + data 
     65        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) 
    5469 
    5570        s.close() 
     
    5772if __name__ == '__main__': 
    5873    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 
    6277 
    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)) 
    6792