-
Notifications
You must be signed in to change notification settings - Fork 1
/
localserver.py
92 lines (75 loc) · 2.98 KB
/
localserver.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
90
91
92
# This script creates a local web server that serves static files in the script's directory.
# In addition to serving static files, it proxies webpbn.com and griddlers.net so that the
# scraper will be able to function.
import sys
from SimpleHTTPServer import SimpleHTTPRequestHandler
import BaseHTTPServer
import urllib
import cgi
# see http://stackoverflow.com/questions/12268835/is-it-possible-to-run-python-simplehttpserver-on-localhost-only
# Request handler that supports simple proxying
class RequestHandler(SimpleHTTPRequestHandler):
# proxied_sites should be a set of websites of this form: ["griddlers.net", "webpbn.com"]
# don't prepend http://www, and don't add any slashes
def __init__(self, request, client_address, server):
self.proxied_sites = set(["webpbn.com", "griddlers.net"])
SimpleHTTPRequestHandler.__init__(self, request, client_address, server)
def should_proxy(self, path):
if self.path.startswith("/__proxy__/"):
start = len("/__proxy__/")
end = self.path.find("/", start)
if end == -1:
end = len(self.path)
website = self.path[start:end]
return website in self.proxied_sites
return False
def generate_real_url(self, path):
start = len("/__proxy__/")
return "http://" + path[start:]
def do_GET(self):
if self.should_proxy(self.path):
real_url = self.generate_real_url(self.path)
self.copyfile(urllib.urlopen(real_url), self.wfile)
else:
SimpleHTTPRequestHandler.do_GET(self)
def do_POST(self):
if self.should_proxy(self.path):
form = cgi.FieldStorage(
fp = self.rfile,
headers = self.headers,
environ = {
"REQUEST_METHOD" : "POST",
"CONTENT_TYPE" : self.headers["Content-Type"]
}
)
post_dict = {}
for key in form:
post_dict[key] = form[key].value
post_data_str = urllib.urlencode(post_dict)
real_url = self.generate_real_url(self.path)
self.copyfile(urllib.urlopen(real_url, post_data_str), self.wfile)
else:
SimpleHTTPRequestHandler.do_POST(self)
def start_server(HandlerClass=RequestHandler,
ServerClass=BaseHTTPServer.HTTPServer):
protocol = "HTTP/1.0"
host = "127.0.0.1"
port = 8000
if len(sys.argv) > 1:
arg = sys.argv[1]
if ':' in arg:
host, port = arg.split(':')
port = int(port)
else:
try:
port = int(sys.argv[1])
except:
host = sys.argv[1]
server_address = (host, port)
HandlerClass.protocol_version = protocol
httpd = ServerClass(server_address, HandlerClass)
sa = httpd.socket.getsockname()
print "Serving HTTP on", sa[0], "port", sa[1], "..."
httpd.serve_forever()
if __name__ == "__main__":
start_server()