| 1 | """Simple interpreter client for monserver |
|---|
| 2 | provides a simple readline interface. |
|---|
| 3 | """ |
|---|
| 4 | |
|---|
| 5 | from warnings import warn |
|---|
| 6 | warn('this module is deprecated and will disappear in a near release', |
|---|
| 7 | DeprecationWarning, stacklevel=1) |
|---|
| 8 | |
|---|
| 9 | from socket import socket, SOCK_STREAM, AF_INET |
|---|
| 10 | from select import select |
|---|
| 11 | import sys |
|---|
| 12 | import readline |
|---|
| 13 | import threading |
|---|
| 14 | |
|---|
| 15 | class SocketPrinter(threading.Thread): |
|---|
| 16 | """A thread that reads from a socket and output |
|---|
| 17 | to stdout as data are received""" |
|---|
| 18 | def __init__(self, sock): |
|---|
| 19 | threading.Thread.__init__(self) |
|---|
| 20 | self.socket = sock |
|---|
| 21 | self.stop = False |
|---|
| 22 | |
|---|
| 23 | def run(self): |
|---|
| 24 | """prints socket input indefinitely""" |
|---|
| 25 | fd = self.socket.fileno() |
|---|
| 26 | self.socket.setblocking(0) |
|---|
| 27 | while not self.stop: |
|---|
| 28 | iwl, _, _ = select([fd], [], [], 2) |
|---|
| 29 | if fd in iwl: |
|---|
| 30 | data = self.socket.recv(100) |
|---|
| 31 | if data: |
|---|
| 32 | sys.stdout.write(data) |
|---|
| 33 | sys.stdout.flush() |
|---|
| 34 | |
|---|
| 35 | |
|---|
| 36 | |
|---|
| 37 | def client( host, port ): |
|---|
| 38 | """simple client that just sends input to the server""" |
|---|
| 39 | sock = socket( AF_INET, SOCK_STREAM ) |
|---|
| 40 | sock.connect( (host, port) ) |
|---|
| 41 | sp_thread = SocketPrinter(sock) |
|---|
| 42 | sp_thread.start() |
|---|
| 43 | while 1: |
|---|
| 44 | try: |
|---|
| 45 | line = raw_input() + "\n" |
|---|
| 46 | sock.send( line ) |
|---|
| 47 | except EOFError: |
|---|
| 48 | print "Bye" |
|---|
| 49 | break |
|---|
| 50 | except: |
|---|
| 51 | sp_thread.stop = True |
|---|
| 52 | sp_thread.join() |
|---|
| 53 | raise |
|---|
| 54 | sp_thread.stop = True |
|---|
| 55 | sp_thread.join() |
|---|
| 56 | |
|---|
| 57 | |
|---|
| 58 | if __name__ == "__main__": |
|---|
| 59 | server_host = sys.argv[1] |
|---|
| 60 | server_port = int(sys.argv[2]) |
|---|
| 61 | client(server_host, server_port) |
|---|
| 62 | |
|---|
| 63 | |
|---|
| 64 | |
|---|