-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
executable file
·49 lines (40 loc) · 1.51 KB
/
main.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
46
47
#!/usr/bin/python
from http.server import BaseHTTPRequestHandler, HTTPServer
import http.client
import zlib
import sys
class RequestHandler(BaseHTTPRequestHandler):
def do_GET(self):
conn = http.client.HTTPSConnection(REMOTE_SERVER)
request_headers = dict()
for header in self.headers:
print(header, ':', self.headers[header])
value = self.headers[header]
if header == "host":
value = REMOTE_SERVER
request_headers[header] = value
if self.command == "GET":
conn.request(self.command, self.path, headers=request_headers)
else:
request_body = self.rfile.read(int(self.headers.getheader('content-length')))
print(request_body)
conn.request(self.command, self.path, request_body, request_headers)
res = conn.getresponse()
self.send_response(res.status)
for header in res.getheaders():
print(header)
self.send_header(header[0], header[1])
self.end_headers()
response_body = res.read()
decompressed_body = response_body
if res.getheader('content-encoding') == 'gzip':
decompressed_body = zlib.decompress(response_body, 31)
print(decompressed_body)
self.wfile.write(response_body)
conn.close()
def do_POST(self):
self.do_GET()
REMOTE_SERVER = sys.argv[1]
SERVER_ADDRESS = ('', 28021)
HTTPD = HTTPServer(SERVER_ADDRESS, RequestHandler)
HTTPD.serve_forever()