example_tcp_client_decoder.py 763 Bytes
Newer Older
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
#!/usr/bin/python

import os
import sys
import socket
import rwsocket

if not any('labcomm' in p for p in sys.path):
    sys.path.append('../../lib/python')
import labcomm


if __name__ == "__main__":
    print "Trying to connect..."
    host = sys.argv[1] #'localhost'
    port = sys.argv[2] #'8081'
    addr = (host,int(port))
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.connect(addr)
    print "Connected!"

    d = labcomm.Decoder(labcomm.StreamReader(sock))

    while True:
        try:
            data,decl = d.decode()
            if data:
                print data
        except KeyError as e:
            print 'KeyError : "%s"' % str(e)
        except:
            print 'exception...', sys.exc_info()[0]
            break