-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproxy.py
executable file
·45 lines (36 loc) · 1.13 KB
/
proxy.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#!/usr/local/bin/python3
# params
local_port = 8080
dest_host = "localhost"
dest_port = 8000
try:
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
except:
from http.server import BaseHTTPRequestHandler, HTTPServer
import requests
# listen on port 8000 of the localhost
addy = ('',local_port)
# define the http handler
class httpd_handler(BaseHTTPRequestHandler):
def do_GET(self):
url = "http://%s:%s%s"%(dest_host, dest_port, self.path)
print ("Fetching %s"%url)
r = requests.get(url)
self.send_response(r.status_code)
for k in r.headers:
self.send_header(k, r.headers[k])
self.end_headers()
self.wfile.write(r.content)
def do_POST(self):
return do_GET(self)
# create http daemon
print("Starting HTTP Proxy -> redirecting port %s to %s:%s"%(local_port, dest_host, dest_port))
httpd = HTTPServer(addy,httpd_handler)
try:
import ssl
httpd.socket = ssl.wrap_socket (httpd.socket,
keyfile="key.pem",
certfile='cert.pem', server_side=True)
except:
print("Error attempting SSL, no HTTPS")
httpd.serve_forever()