Spaces:
Paused
Paused
#!/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) | |
if 'Host' in headers: | |
del headers['Host'] # Remove "Host" header to avoid "HTTP/1.1 400 Bad Request" error | |
if 'host' in headers: | |
del headers['host'] # Remove "Host" header to avoid "HTTP/1.1 400 Bad Request" error | |
conn = http.client.HTTPConnection('localhost', PROXY_PORT) | |
print(headers) | |
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() | |