Python requests库Proxy代理问题

攻克伟大的防火墙需要借助代理。在网络请求中,代理服务器充当我们request的服务器,而后代理服务器充当客户端,向墙外服务器发送request。response通过代理服务器转发给我们。

比如说访问facebook:

1.启动一个代理服务

2.在主机的1080端口启动Socks服务

3.把请求转发到1080端口

4.获取请求资源

代码实现:

开发环境中安装 pip install 'requests[socksv5]'

proxies = {"http": "socks5://127.0.0.1:1080","https": "socks5://127.0.0.1:1080",}

response=requests.get("https://www.facebook.com",proxies=proxies,timeout=10)

因为我购买的代理服务器在本地浏览器更改了代理服务设置,使用的是http协议,所以在我的开发过程中这样写成功返回 200 OK:

proxies = {"http": "http://127.0.0.1:1080","https": "http://127.0.0.1:1080",}

response=requests.get(url,proxies=proxies,timeout=10)


猜你喜欢

转载自blog.csdn.net/weixin_41431904/article/details/80732397