智能家居DIY实战:用STM32和ESP8266将温湿度数据上传到OneNET(附微信小程序控制思路)
2026/4/21 21:48:44
PAC(Proxy Auto-Config)文件是一个简单的文本文件,告诉浏览器:访问哪些网站走代理,哪些网站直接连接。
这样就不需要手动切换代理,全部自动完成!
# Created by johnny on 20240412importosimporthttp.serverimportsocketserver PORT=22222PACFILE_NAME="proxy.pac"defprepare_pac():dir_path=os.path.dirname(os.path.abspath(__file__))pacfile_path=os.path.join(dir_path,PACFILE_NAME)try:withopen(pacfile_path,'rb')asf:returnf.read()exceptFileNotFoundError:print(f"File{pacfile_path}not found.")returnNoneclassPacRequestHandler(http.server.SimpleHTTPRequestHandler):defdo_GET(self):ifself.path=='/'+PACFILE_NAME:pac_content=prepare_pac()ifpac_contentisNone:self.send_error(404)returnself.send_response(200)self.send_header('Content-type','application/x-ns-proxy-autoconfig')self.end_headers()self.wfile.write(pac_content)else:self.send_error(404)classThreadingTCPServer(socketserver.ThreadingTCPServer):allow_reuse_address=Truedefmain():Handler=PacRequestHandlerwithThreadingTCPServer(("10.11.12.222",PORT),Handler)ashttpd:print("Serving PAC file on port",PORT)try:httpd.serve_forever()exceptKeyboardInterrupt:print("Shutting down server...")httpd.shutdown()if__name__=='__main__':main()在同一个文件夹下创建proxy.pac文件,内容如下:
functionFindProxyForURL(url,host){// 1. 公司内部网站直接访问if(dnsDomainIs(host,".company.com")||shExpMatch(host,"*.local")){return"DIRECT";}// 2. 国内网站直接访问if(shExpMatch(host,"*.baidu.com")||shExpMatch(host,"*.taobao.com")||shExpMatch(host,"*.cn")){return"DIRECT";}// 3. 国外网站走代理if(shExpMatch(host,"*.google.com")||shExpMatch(host,"*.youtube.com")){return"PROXY 10.11.12.222:8080";}// 4. 其他的默认直连return"DIRECT";}说明:
DIRECT= 直接连接,不走代理PROXY 10.11.12.222:8080= 走代理服务器(请改成你的代理地址)把下面两个文件放在同一个文件夹里:
/opt/pac-server/ ├── agent-pac.py # 主程序 └── proxy.pac # 代理规则用记事本打开agent-pac.py,找到这一行:
withThreadingTCPServer(("10.11.12.222",PORT),Handler)ashttpd:把"10.11.12.222"改成你电脑的实际 IP 地址。
在终端里执行命令:
nohuppython3 agent-pac.py&命令说明:
nohup:让程序在后台运行,关掉终端也不会停止&:表示后台运行netstat-tnpl|grep22222如果看到类似这样的输出,说明启动成功了:
tcp 0 0 10.11.12.222:22222 0.0.0.0:* LISTEN 12345/python3# 后台启动(推荐)nohuppython3 agent-pac.py&# 后台启动并记录日志到指定文件nohuppython3 agent-pac.py>pac.log2>&1&# 查看服务是否在运行netstat-tnpl|grep22222# 查看进程psaux|grepagent-pac# 查看日志tail-f nohup.out# 查找进程号并停止psaux|grepagent-packill<进程号># 或者一键停止pkill-f agent-pac.py# 在服务器上测试curlhttp://10.11.12.222:22222/proxy.pac# 在其他电脑上测试(确保网络连通)curlhttp://10.11.12.222:22222/proxy.pachttp://10.11.12.222:22222/proxy.pachttp://10.11.12.222:22222/proxy.pachttp://10.11.12.222:22222/proxy.pacChrome 使用系统代理,设置好系统代理即可。
也可以用命令行启动:
chrome.exe --proxy-pac-url="http://10.11.12.222:22222/proxy.pac"http://10.11.12.222:22222/proxy.pac问题:公司内部系统和外部网站都要访问,手动切换代理很麻烦。
解决:
// 公司内网直接访问if(dnsDomainIs(host,".company.com")){return"DIRECT";}// 外网走公司代理return"PROXY company-proxy.com:8080";问题:想自动区分国内网站和国外网站,国内直连,国外走代理。
解决:
// 国内网站直连if(shExpMatch(host,"*.baidu.com")||shExpMatch(host,"*.cn")){return"DIRECT";}// 其他走代理(访问国外网站)return"PROXY 你的代理服务器:端口";问题:开发和生产环境用不同的代理。
解决:
if(shExpMatch(host,"*.test.com")){return"PROXY test-proxy:8080";}if(shExpMatch(host,"*.prod.com")){return"PROXY prod-proxy:8080";}解决:
# 查看谁占用了 22222 端口netstat-tlnp|grep22222# 杀掉占用进程kill<进程号># 或者修改端口(修改 agent-pac.py 里的 PORT = 22222)排查:
curl http://localhost:22222/proxy.pacping 10.11.12.222原因:浏览器会缓存 PAC 文件。
解决:
chrome://net-internals/#proxy点击重新加载方法:编辑proxy.pac文件,添加新的规则,保存即可。
示例:
// 让 GitHub 走代理if(shExpMatch(host,"*.github.com")){return"PROXY 10.11.12.222:8080";}# 查找进程psaux|grepagent-pac# 你会看到类似这样的输出:# root 12345 0.0 0.1 ... python3 agent-pac.py# 停止进程(12345 是进程号)kill12345agent-pac.py # 主程序(启动服务) proxy.pac # 代理规则(定义哪些网站走代理)cd/opt/pac-servernohuppython3 agent-pac.py&netstat-tnpl|grep22222http://10.11.12.222:22222/proxy.pac(请把 IP 改成你的实际服务器 IP)