-
Notifications
You must be signed in to change notification settings - Fork 1
/
relay.py
executable file
·80 lines (74 loc) · 1.85 KB
/
relay.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/usr/bin/python
import socket
import httplib
import urllib
import re
C_PORT = 45987 #Port used by telemetry unit
C_ADDR = None
S_PORT = 80 #Port used to connect to the webserver
S_ADDR = "81.167.78.33" #Server address
PATH = "/ecomarathon/setdata.php"
for res in socket.getaddrinfo(C_ADDR, C_PORT, socket.AF_UNSPEC, socket.SOCK_STREAM, 0, socket.AI_PASSIVE):
print(res)
c_f, c_st, c_p, c_cn, c_sa = res
c_srv_soc = socket.socket(c_f, c_st, c_p)
c_srv_soc.bind(c_sa)
c_srv_soc.listen(1)
break
data = ""
while 1:
try:
car_soc, addr = c_srv_soc.accept()
except KeyboardInterrupt:
c_srv_soc.close()
break
except:
c_srv_soc.close()
continue
car_soc.settimeout(20.0);
try:
while 1:
data = ""
temp = ""
while not re.search("\n", data):
temp = car_soc.recv(1024)
data += temp
if not temp:
data = ""
break
if not data:
print("no data")
car_soc.close()
break
else:
print(data)
print("got some\n")
pattern = "G,\d*,\d*,\d*,\d*,\d*,\d*,\d*,\d*"
pos = re.search(pattern, data)
if pos:
pos = pos.group(0).split(",",9)
lat = int(pos[1]) * 1.0
lat += int(pos[2]) / 60.0
lat += int(pos[3]) / 6000.0
lat += int(pos[4]) / 600000.0
lon = int(pos[5]) * 1.0
lon += int(pos[6]) / 60.0
lon += int(pos[7]) / 6000.0
lon += int(pos[8]) / 600000.0
data = re.sub(pattern, "G," + str(lat) + "," + str(lon), data)
print(data)
h1 = httplib.HTTPConnection(S_ADDR, S_PORT)
h1.request("POST", PATH, urllib.urlencode({"data": data}), {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"})
response = h1.getresponse()
print(response)
if response.status != 200:
print("http error")
break
data = response.read()
data = ""
h1.close
car_soc.close()
except:
car_soc.close()
continue
c_srv_soc.close()