forked from marcelomd/mux_serial
-
Notifications
You must be signed in to change notification settings - Fork 1
/
mux_client.py
executable file
·56 lines (43 loc) · 1.2 KB
/
mux_client.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#! /usr/bin/env python
import sys
import socket
import fdpexpect
import atexit
_default_host = 'localhost'
_default_port = 23200
class MuxClient(object):
def __init__(self, host = _default_host, port = _default_port):
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.host = host
self.port = port
self.term = None
def run(self):
self.sock.connect_ex((self.host, self.port))
self.term = fdpexpect.fdspawn(self.sock)
print >>sys.stderr, 'MUX > Connected to %s:%d' % (self.host, self.port)
return self.term
def interact(self):
print >>sys.stderr, 'MUX > Use ctrl+] to stop...\n'
self.term.interact()
def close(self):
print >>sys.stderr, '\nMUX > Closing...'
if self.term is None:
self.sock.close()
else:
self.term.close() # Closes sock too
print >>sys.stderr, 'MUX > Done! =)'
if __name__ == '__main__':
import optparse
# Option parsing, duh
parser = optparse.OptionParser()
parser.add_option('-p', '--port',
help = 'Host port',
dest = 'port',
type = 'int',
default = _default_port)
(opts, args) = parser.parse_args()
client = MuxClient(port=opts.port)
client.run()
client.interact()
if not sys.flags.interactive:
client.close()