root / logilab.pylintinstaller / logilab / common / monclient.py

Revision 202:d67e86292521, 1.6 kB (checked in by tziade@…, 9 months ago)

added logilab.pylintinstaller

Line 
1"""Simple interpreter client for monserver
2provides a simple readline interface.
3"""
4
5from warnings import warn
6warn('this module is deprecated and will disappear in a near release',
7     DeprecationWarning, stacklevel=1)
8
9from socket import socket, SOCK_STREAM, AF_INET
10from select import select
11import sys
12import readline
13import threading
14
15class 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
37def 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
58if __name__ == "__main__":
59    server_host = sys.argv[1]
60    server_port = int(sys.argv[2])
61    client(server_host, server_port)
62
63       
64       
Note: See TracBrowser for help on using the browser.