Created
January 28, 2010 19:27
-
-
Save acdha/289052 to your computer and use it in GitHub Desktop.
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
#!/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