Spaces:
Paused
Paused
File size: 1,210 Bytes
30a9d8e c20a286 30a9d8e c20a286 30a9d8e |
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 |
#!/usr/bin/env python3
import http.server
import http.client
import socketserver
PORT = 8000
PROXY_PORT = 8080
class ProxyRequestHandler(http.server.SimpleHTTPRequestHandler):
def do_GET(self):
# Forward GET request to the proxy port with "/site" appended to the path
if self.path == '/' or self.path.startswith("/?"):
url = 'http://localhost:{}{}'.format(PROXY_PORT, '/site' + self.path)
else:
url = 'http://localhost:{}{}'.format(PROXY_PORT, self.path)
headers = dict(self.headers)
del headers['Host'] # Remove "Host" header to avoid "HTTP/1.1 400 Bad Request" error
conn = http.client.HTTPConnection('localhost', PROXY_PORT)
conn.request('GET', url, headers=headers)
response = conn.getresponse()
self.send_response(response.status)
for header, value in response.getheaders():
self.send_header(header, value)
self.end_headers()
self.wfile.write(response.read())
if __name__ == '__main__':
# Start the HTTP server
with socketserver.TCPServer(("", PORT), ProxyRequestHandler) as httpd:
print("Server running on port", PORT)
httpd.serve_forever()
|