File size: 1,286 Bytes
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
33
34
35
36
#!/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 == '/':
        		url = 'http://localhost:{}{}'.format(PROXY_PORT, '/site' + self.path)
        else:
        		url = 'http://localhost:{}{}'.format(PROXY_PORT, self.path)
        print("path={}, url={}".format(self.path,url))
        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()

        # Send the proxy response back to the client
        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()