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
|
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", 9999
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 send(data):
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((HOST, PORT))
print "sent."
sock.sendall(data)
sock.shutdown(socket.SHUT_WR)
received = sock.recv(1024)
finally:
print "adf"
sock.close()
print "Sent".format(get_hex(data))
print "Received: {}".format(get_hex(data))
return received
rec = send(gen_auth("hello", "world"))
opt, token, status = unpack("!BLB", rec)
print "status:" + str(status)
for i in range(10):
rec = send(gen_update_location(token, random(), random()))
opc, status = unpack("!BB", rec)
print "status:" + str(status)
sleep(10)
|