Created
May 4, 2014 16:00
-
-
Save dimtion/11518964 to your computer and use it in GitHub Desktop.
Python cache proxy for Etienne
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Originally from | |
# http://sharebear.co.uk/blog/2009/09/17/very-simple-python-caching-proxy/ | |
# | |
# Usage: | |
# A call to http://localhost:80000/http://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. | |
# Improved with love by @dimtion and @tikiki | |
# Version: 0.2 | |
# Add: possibility to cache multiple domains, see : cache_spread | |
# TODO | |
# Handle HTTP POST REQUESTS | |
# Handle other protocols such as DNS, webSocket etc... | |
import BaseHTTPServer | |
import hashlib | |
import os | |
import urllib2 | |
class CacheHandler(BaseHTTPServer.BaseHTTPRequestHandler): | |
cache_spread = ('srv956.sd-france.net','go.microsoft.com') | |
def do_GET(self): | |
m = hashlib.md5() | |
m.update(self.path) | |
cache_filename = 'cached/'+m.hexdigest() + ".cached" | |
if os.path.exists(cache_filename): | |
print "Cache hit" | |
data = open(cache_filename).readlines() | |
else: | |
print "Cache miss" | |
print self.path | |
data = urllib2.urlopen(self.path).readlines() | |
if any(serv in self.path for serv in self.cache_spread): | |
print "To cache" | |
open(cache_filename, 'wb').writelines(data) | |
self.send_response(200) | |
self.end_headers() | |
self.wfile.writelines(data) | |
def run(): | |
server_address = ('', 8000) | |
httpd = BaseHTTPServer.HTTPServer(server_address, CacheHandler) | |
httpd.serve_forever() | |
if __name__ == '__main__': | |
print("-- Server starts --") | |
run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment