用python写一个反向代理,给个简单的示例看看?
搜了好久没有搜到。。。
===========================================
最后还是搜到了。
import BaseHTTPServer
import hashlib
import os
import urllib2
class CacheHandler(BaseHTTPServer.BaseHTTPRequestHandler):
def do_GET(self):
m = hashlib.md5()
m.update(self.path)
cache_filename = m.hexdigest()
if os.path.exists(cache_filename):
print "Cache hit"
data = open(cache_filename).readlines()
else:
print "Cache miss"
data = urllib2.urlopen("http://targetserver" + self.path).readlines()
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__':
run()
=============================
应该是我描述的不清楚。这个基本上就是我想要的了。
http://sharebear.co.uk/blog/2009/09/1...