root / trunk / code / projects / mapping / python / colony.py @ 918
History | View | Annotate | Download (913 Bytes)
| 1 | #!/usr/bin/env python3.0 |
|---|---|
| 2 | import sys, time |
| 3 | from socket import * |
| 4 | from struct import * |
| 5 | |
| 6 | hostname='localhost' |
| 7 | |
| 8 | def inp(str): |
| 9 | return input(str).encode('ascii')
|
| 10 | |
| 11 | def main(): |
| 12 | |
| 13 | funcs = {1 : 'print_range_info'}
|
| 14 | |
| 15 | buf = [] |
| 16 | sock = socket() |
| 17 | sock.connect((hostname, int(sys.argv[1]))) |
| 18 | |
| 19 | while (True): |
| 20 | buf = sock.recv(100) |
| 21 | |
| 22 | if (buf): |
| 23 | type = buf[2] |
| 24 | if type in funcs.keys(): |
| 25 | globals()[funcs[type]](buf) |
| 26 | |
| 27 | |
| 28 | def print_range_info(packet): |
| 29 | print ('Info from %d' % packet[0])
|
| 30 | |
| 31 | src = packet[0] |
| 32 | |
| 33 | packet_length = packet[1] |
| 34 | |
| 35 | data = packet[3 : 3+packet_length] |
| 36 | print("data length: %d" % len(data))
|
| 37 | ranges = unpack('hhhhhhh',data)
|
| 38 | |
| 39 | print(ranges) |
| 40 | |
| 41 | |
| 42 | if __name__=='__main__': |
| 43 | |
| 44 | if len(sys.argv) < 2 or not str.isdecimal(sys.argv[1]): |
| 45 | print("Usage: %s <portno>" % sys.argv[0])
|
| 46 | sys.exit() |
| 47 | |
| 48 | main() |