-
Notifications
You must be signed in to change notification settings - Fork 240
/
proxy.py
70 lines (56 loc) · 2.04 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
# Originally from http://sharebear.co.uk/blog/2009/09/17/very-simple-python-caching-proxy/
#
# Usage:
# A call to http://localhost:8000/example.com/foo.html will cache the file
# at http://example.com/foo.html on disc and not redownload it again.
# To clear the cache simply do a `rm *.cached`. To stop the server simply
# send SIGINT (Ctrl-C). It does not handle any headers or post data.
#
# see also: https://pymotw.com/2/BaseHTTPServer/
import BaseHTTPServer
# import hashlib
import os
import urllib2
from BaseHTTPServer import HTTPServer
from SocketServer import ThreadingMixIn
import tempfile
class ThreadedHTTPServer(ThreadingMixIn, HTTPServer):
pass
class CacheHandler(BaseHTTPServer.BaseHTTPRequestHandler):
def ok(self):
self.send_response(200)
self.send_header("Content-Encoding", "gzip")
self.end_headers()
def do_GET(self):
dirname = ".tiles/" + os.path.dirname(self.path)[1:]
filename = os.path.basename(self.path)
while not os.path.exists(dirname):
# might be a race here
try:
os.makedirs(dirname)
except:
None
cache_filename = dirname + "/" + filename
if os.path.exists(cache_filename):
data = open(cache_filename, mode='rb').readlines()
self.ok()
self.wfile.writelines(data)
return
print "fetching: %s" % (cache_filename)
data = urllib2.urlopen("http:/" + self.path, timeout=10).readlines()
self.ok()
self.wfile.writelines(data)
f = tempfile.NamedTemporaryFile(dir=os.path.dirname(cache_filename),
mode='wb',
delete=False)
f.writelines(data)
f.close()
os.rename(f.name, cache_filename)
def run():
if not os.path.exists(".tiles"):
os.makedirs(".tiles")
server_address = ('', 8000)
httpd = ThreadedHTTPServer(server_address, CacheHandler)
httpd.serve_forever()
if __name__ == '__main__':
run()