Simple HTTP Proxy with Python: Difference between revisions
(Created page with "<source lang="python"> #!/usr/bin/python3 import os, sys # from urllib.parse import urlparse # from urllib.request import urlopen import urllib.request # import cgi # cgi.pr...") |
No edit summary |
||
Line 1: | Line 1: | ||
mkdir cgi-bin | |||
nano cgi-bin/proxy.cgi | |||
And paste: | |||
<source lang="python"> | <source lang="python"> | ||
#!/usr/bin/python3 | #!/usr/bin/python3 |
Revision as of 16:29, 9 October 2018
mkdir cgi-bin nano cgi-bin/proxy.cgi
And paste:
#!/usr/bin/python3
import os, sys
# from urllib.parse import urlparse
# from urllib.request import urlopen
import urllib.request
# import cgi
# cgi.print_environ()
path_info = os.environ.get("PATH_INFO", "").lstrip("/")
query_string = os.environ.get("QUERY_STRING", "")
url = "https://"+path_info+"?"+query_string
print ("opening", url, file=sys.stderr)
req = urllib.request.Request(
url,
headers={
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.47 Safari/537.36'
})
f = urllib.request.urlopen(req)
ct = f.info().get("content-type")
print ("Serving data with content-type", ct, file=sys.stderr)
header = ct+"\n\n"
sys.stdout.buffer.write(header.encode("utf-8"))
while True:
bytes = f.read()
if bytes:
sys.stdout.buffer.write(bytes)
else:
break
sys.stdout.buffer.flush()
Running (on port 8090)
python -m http.server 8090 --cgi