diff options
author | Teddy <ted.sybil@gmail.com> | 2013-08-25 17:55:34 +0800 |
---|---|---|
committer | Teddy <ted.sybil@gmail.com> | 2013-08-25 17:55:34 +0800 |
commit | f4d0989ae888bc2f83f0bc09cc826f7c79b1b6b5 (patch) | |
tree | 79c9fbb49a5a857c56843cef01b032e3430aa085 /misc/server/client.py | |
parent | f74999631c4f83a0c8532d6b7adb348dcd5d5205 (diff) | |
parent | 0a76dad753ed88a7575c2aafdd068ef6caa7247f (diff) |
Merge branch 'master' of github.com:Determinant/piztor
Diffstat (limited to 'misc/server/client.py')
-rw-r--r-- | misc/server/client.py | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/misc/server/client.py b/misc/server/client.py new file mode 100644 index 0000000..15f4bbc --- /dev/null +++ b/misc/server/client.py @@ -0,0 +1,58 @@ +import socket +import sys +from struct import * +from random import random +from time import sleep + +def get_hex(data): + return "".join([hex(ord(c))[2:].zfill(2) for c in data]) + +HOST, PORT = "localhost", 9990 + +def gen_auth(username, password): + data = pack("!B", 0) + data += username + data += "\0" + data += password + return data + +def gen_update_location(token, lat, lont): + return pack("!BLdd", 2, token, lat, lont) + +def gen_request_location(token, gid): + return pack("!BLL", 3, token, gid) + +def send(data): + try: + sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + sock.connect((HOST, PORT)) +# print "Client " + str(sys.argv[1]) + ": connected" + sock.sendall(data) + print get_hex(data) +# print "Client " + str(sys.argv[1]) + ": sent" +# sock.shutdown(socket.SHUT_WR) +# print "Client " + str(sys.argv[1]) + ": shutdown" + received = sock.recv(1024) + finally: + print "adf" + sock.close() + + print "Sent {}".format(get_hex(data)) + print "Received: {}".format(get_hex(data)) + return received + +#print "Client spawned:" + str(sys.argv[1]) +rec = send(gen_auth("hello", "world")) +opt, token, status = unpack("!BLB", rec) + +rec = send(gen_update_location(token, random(), random())) +opc, status = unpack("!BB", rec) + +rec = send(gen_request_location(token, 1)) +opc, length = unpack("!BL", rec[:5]) +idx = 5 +for i in xrange(length): + uid, lat, lng = unpack("!Ldd", rec[idx:idx + 20]) + print (uid, lat, lng) + idx += 20 +# sleep(60) |