Skip to content

Instantly share code, notes, and snippets.

@acdha
Created January 28, 2010 19:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save acdha/289052 to your computer and use it in GitHub Desktop.
Save acdha/289052 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
"""
Quick and dirty filtering HTTP proxy using only Python stdlib
Change REMOTE_HOST below and adjust the regexp to suit your needs
"""
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
from urllib import urlopen, basejoin
import re
class FilteringProxy(BaseHTTPRequestHandler):
"""
Template to easily allow filtering HTTP responses
"""
def do_GET(self):
remote = urlopen(basejoin("http://REMOTE_HOST/", self.path))
self.send_response(remote.code, 'OK')
for k, v in remote.info().items():
self.send_header(k, v)
self.end_headers()
content = remote.read()
content = re.sub(
r'<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">',
"",
content
)
self.wfile.write(content)
@staticmethod
def serve_forever():
s = HTTPServer(('', 8080), FilteringProxy).serve_forever()
if __name__ == "__main__":
FilteringProxy.serve_forever()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment