Skip to main content
 首页 » 编程设计

python之使用 https 代理请求

2024年10月01日11telwanggs

我正在使用 python 请求加载使用 https 代理的页面,但它不工作。相同的代码适用于 http 代理。

以下是我的代码:

使用 https 代理

import requests 
proxyDict = {'https': 'https://xxx.xxx.xxx.xxx:xx'} 
r = requests.get('http://icanhazip.com', proxies=proxyDict) 
print(r) 
print(r.content) 

Output:

Response [200]

xxx.xxx.xxx.xxx - the ip is for my server and not the proxy server

使用http代理

import requests 
proxyDict = {'http': 'http://xxx.xxx.xxx.xxx:xx'} 
r = requests.get('http://icanhazip.com', proxies=proxyDict) 
print(r) 
print(r.content) 

Output:

Response [200]

xxx.xxx.xxx.xxx - the ip is for proxy server

如何将https代理服务器与python请求一起使用?

我的代码有错误吗?或者如何解决这个问题?

请帮我解决这个问题。

请您参考如下方法:

在您的第一个代码示例中,您使用 HTTPS 代理但向 HTTP 站点发出请求。您应该改为执行 r = requests.get('https://icanhazip.com', proxies=proxyDict)。