Newer
Older
Import / applications / HighwayDash / ports / Tools / DebugSocket.py
#!/usr/bin/python

import socket, select, string, sys

if __name__ == "__main__":
    if (len(sys.argv) < 3):
        print 'Usage : python DebugSocket.py hostname port'
        sys.exit()

    host = sys.argv[1]
    port = int(sys.argv[2])
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.settimeout(2)

    # connect to remote host
    try:
        s.connect((host, port))
    except:
        print 'Unable to connect'
        sys.exit()
    print 'Connected to remote host'

    while 1:
        socket_list = [sys.stdin, s]
 
        # list sockets which are readable
        read_sockets, write_sockets, error_sockets = select.select(socket_list , [], [])
        for sock in read_sockets:
            # incoming message from remote server
            if sock == s:
                data = sock.recv(4096)
                if not data:
                    print '\nDisconnected from server'
                    sys.exit()
                sys.stdout.write(data)
                sys.stdout.flush()
            # user entered a message
            else:
                msg = sys.stdin.readline()
                if msg == 'quit\n':
                    s.close()
                    sys.exit()
                s.send(msg)