File tree 2 files changed +52
-0
lines changed
2 files changed +52
-0
lines changed Original file line number Diff line number Diff line change 12
12
# Thumbnails
13
13
._ *
14
14
15
+ # proxy.py cache
16
+ .tiles
17
+
15
18
# Files that might appear on external disk
16
19
.Spotlight-V100
17
20
.Trashes
Original file line number Diff line number Diff line change
1
+ # Originally from http://sharebear.co.uk/blog/2009/09/17/very-simple-python-caching-proxy/
2
+ #
3
+ # Usage:
4
+ # A call to http://localhost:8000/example.com/foo.html will cache the file
5
+ # at http://example.com/foo.html on disc and not redownload it again.
6
+ # To clear the cache simply do a `rm *.cached`. To stop the server simply
7
+ # send SIGINT (Ctrl-C). It does not handle any headers or post data.
8
+ #
9
+ # see also: https://pymotw.com/2/BaseHTTPServer/
10
+
11
+ import BaseHTTPServer
12
+ import hashlib
13
+ import os
14
+ import urllib2
15
+
16
+ from BaseHTTPServer import HTTPServer
17
+ from SocketServer import ThreadingMixIn
18
+
19
+ class ThreadedHTTPServer (ThreadingMixIn , HTTPServer ):
20
+ pass
21
+
22
+ class CacheHandler (BaseHTTPServer .BaseHTTPRequestHandler ):
23
+ def do_GET (self ):
24
+ m = hashlib .md5 ()
25
+ m .update (self .path )
26
+ cache_filename = ".tiles/" + m .hexdigest ()
27
+ if os .path .exists (cache_filename ):
28
+ print "Cache hit"
29
+ data = open (cache_filename ).readlines ()
30
+ else :
31
+ print "Cache miss"
32
+ data = urllib2 .urlopen ("http:/" + self .path , timeout = 10 ).readlines ()
33
+ open (cache_filename , 'wb' ).writelines (data )
34
+
35
+ self .send_response (200 )
36
+ self .send_header ("Content-Encoding" , "gzip" )
37
+ self .end_headers ()
38
+ self .wfile .writelines (data )
39
+
40
+ def run ():
41
+ if not os .path .exists (".tiles" ):
42
+ os .makedirs (".tiles" )
43
+
44
+ server_address = ('' , 8000 )
45
+ httpd = ThreadedHTTPServer (server_address , CacheHandler )
46
+ httpd .serve_forever ()
47
+
48
+ if __name__ == '__main__' :
49
+ run ()
You can’t perform that action at this time.
0 commit comments