import urllib2 import BaseHTTPServer, SimpleHTTPServer class SimpleHTTPProxy(SimpleHTTPServer.SimpleHTTPRequestHandler, object): proxy_routes = {} @classmethod def set_routes(cls, proxy_routes): cls.proxy_routes = proxy_routes def do_GET(self): parts = self.path.split('/') if len(parts) >= 2 and parts[1] in self.proxy_routes: url = self.proxy_routes[parts[1]] + '/'.join(parts[2:]) self.porxy_request(url) else: super(SimpleHTTPProxy, self).do_GET() def porxy_request(self, url): try: print url response = urllib2.urlopen(url) except urllib2.HTTPError as e: self.send_response(e.code) self.end_headers() return self.send_response(response.getcode()) for name, value in response.headers.items(): self.send_header(name, value) self.end_headers() self.copyfile(response, self.wfile)