Spaces:
Paused
Paused
Create proxy.py
Browse files
proxy.py
ADDED
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/usr/bin/env python3
|
2 |
+
import http.server
|
3 |
+
import http.client
|
4 |
+
import socketserver
|
5 |
+
|
6 |
+
PORT = 8000
|
7 |
+
PROXY_PORT = 8080
|
8 |
+
|
9 |
+
class ProxyRequestHandler(http.server.SimpleHTTPRequestHandler):
|
10 |
+
def do_GET(self):
|
11 |
+
# Forward GET request to the proxy port with "/site" appended to the path
|
12 |
+
|
13 |
+
if self.path == '/':
|
14 |
+
url = 'http://localhost:{}{}'.format(PROXY_PORT, '/site' + self.path)
|
15 |
+
else:
|
16 |
+
url = 'http://localhost:{}{}'.format(PROXY_PORT, self.path)
|
17 |
+
print("path={}, url={}".format(self.path,url))
|
18 |
+
headers = dict(self.headers)
|
19 |
+
del headers['Host'] # Remove "Host" header to avoid "HTTP/1.1 400 Bad Request" error
|
20 |
+
conn = http.client.HTTPConnection('localhost', PROXY_PORT)
|
21 |
+
conn.request('GET', url, headers=headers)
|
22 |
+
response = conn.getresponse()
|
23 |
+
|
24 |
+
# Send the proxy response back to the client
|
25 |
+
self.send_response(response.status)
|
26 |
+
for header, value in response.getheaders():
|
27 |
+
self.send_header(header, value)
|
28 |
+
self.end_headers()
|
29 |
+
self.wfile.write(response.read())
|
30 |
+
|
31 |
+
if __name__ == '__main__':
|
32 |
+
# Start the HTTP server
|
33 |
+
with socketserver.TCPServer(("", PORT), ProxyRequestHandler) as httpd:
|
34 |
+
print("Server running on port", PORT)
|
35 |
+
httpd.serve_forever()
|