forked from StianF/ecomarathon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimrelay.py
executable file
·72 lines (69 loc) · 1.64 KB
/
simrelay.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
#!/usr/bin/python2
import socket
import httplib
import urllib
import re
C_PORT = 45988 #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
S_ADDR = "localhost"
PATH = "/eco/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(3)
break
data = ""
while 1:
try:
print("Accepting")
car_soc, addr = c_srv_soc.accept()
except KeyboardInterrupt:
c_srv_soc.close()
break
except:
c_srv_soc.close()
print("failed to accept")
continue
car_soc.settimeout(20.0);
print("Timeout set")
try:
while 1:
data = ""
temp = ""
while not re.search("\n", data):
temp = car_soc.recv(1024)
print("Received :" + temp)
data += temp
if not temp:
data = ""
break
if not data:
print("no data")
car_soc.close()
break
else:
print(data)
print("got some\n")
try:
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
except Exception as exception:
print("Post failed: " + string(exception))
car_soc.close()
except Exception as exception:
print(exception)
car_soc.close()
continue
c_srv_soc.close()