一个简单的Python反代代码

  • 一个简单的Python反代代码已关闭评论
  • 18 views
  • A+
所属分类:www

快速反代的Python代码,有缓存功能,所以运行的时候会在当前目录生成很多缓存文件,默认端口3000 url=http://127.0.0.1:3000

运行命令: Python proxy.py

vi proxy.py


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("https://www.youtube.com" + self.path).readlines()
          open(cache_filename, 'wb').writelines(data)
      self.send_response(200)
      self.end_headers()
      self.wfile.writelines(data)

def run():
    server_address = ('', 3000)
    httpd = BaseHTTPServer.HTTPServer(server_address, CacheHandler)
    httpd.serve_forever()

if __name__ == '__main__':
    run()