-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserve_proxy.py
More file actions
36 lines (33 loc) · 1.29 KB
/
serve_proxy.py
File metadata and controls
36 lines (33 loc) · 1.29 KB
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
# 只暴露 /proxy.pac,浏览器访问直接显示 UTF-8 内容
from http.server import HTTPServer, BaseHTTPRequestHandler
PAC_FILE = "/data/proxy.pac"
PORT = 8080
class ProxyHandler(BaseHTTPRequestHandler):
def do_GET(self):
if self.path == "/":
# 访问根路径自动跳转到 /proxy.pac
self.send_response(302)
self.send_header("Location", "/proxy.pac")
self.end_headers()
return
if self.path != "/proxy.pac":
self.send_response(403)
self.end_headers()
self.wfile.write(b"Forbidden\n")
return
try:
with open(PAC_FILE, "r", encoding="utf-8") as f:
content = f.read()
self.send_response(200)
self.send_header("Content-Type", "text/plain; charset=utf-8")
self.send_header("Content-Length", str(len(content.encode("utf-8"))))
self.end_headers()
self.wfile.write(content.encode("utf-8"))
except Exception as e:
self.send_response(500)
self.end_headers()
self.wfile.write(str(e).encode("utf-8"))
httpd = HTTPServer(("", PORT), ProxyHandler)
print(f"Serving proxy.pac on port {PORT}, UTF-8, inline display")
httpd.serve_forever()