-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproxy.py
89 lines (73 loc) · 2.29 KB
/
proxy.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
81
82
83
84
85
86
87
88
89
#!/usr/bin/python
import serial
from bottle import route, run, template, response, static_file
import config
port = serial.Serial(config.node_serial_port, baudrate=115200,timeout=60)
returnheadder=""
returndata=""
def fetchurl(URL):
port.write(URL+"\n") #send request to node
print "Fetching URL: "+URL
data = port.readline(10000) #read the first respeonse line
global returnheadder
global returndata
returnheadder="" #clear any previous headders
returndata="" #clear any previous data
fetchstate="sent" #mark that request has been sent
while data != "":
#print repr(data)
if fetchstate == "sent": #ignore initial statements
print repr(data)
if data[0:5] == "HTTP/": #now receiveing header
fetchstate="headder" #start saving headder
if data == "Could not establish connection\n":
return False
if fetchstate == "headder": #save headders for later processing
#print "headder"
if data=="\r\n":
fetchstate="data"
#print "now for the data"
else:
returnheadder+=data #store headder details
elif fetchstate=="data":
#print "data :"
if data=="Connection closed.\n": #if connection has been closed
fetchstate="done" #process any remaining debug messages
else:
returndata+=data #store the data
elif fetchstate=="done":
#process final codes
if data[0:8] == "Status = 200\n"[0:8]: #look for an end condition
return int(data[9:12]) #return success
data = port.readline(10000) #read the next line for processing
return False
def update_cache(url):
try:
f=open('cache/'+url,'w')
f.write(returndata)
f.close()
except:
print "unable to update cache for "+url
@route('/node/<url:path>')
def nodepages(url):
if (fetchurl(url)):
update_cache(url)
return returndata
else:
response.status=504
return "unable to comunicate with node page might be <a href=\"../cache/"+url+"\">cached</a>"
@route('/cache/<url:path>')
def send_cache(url):
return static_file(url, root='cache/')
@route('/')
def index():
#return "<html><body><p><a href=\"/node/"+config.Border_Router_IP+"\">Border Router page</a></p></html>"
return static_file("index.html", "")
if __name__ == "__main__":
# print fetchurl("2001:630:d0:f200:212:7400:1465:d8aa")
#
# print "headder"
# print returnheadder
# print "data"
# print returndata
run(host="localhost", port=8080)