进行request请求时,有时必须开VPN才能访问,而开着会遇到如下问题:

原因是urllib3升级后增加了 HTTPS的支持,就尝试用https连接代理服务器,但是代理服务器其实只支持 http,因此出现了错误。

简单解决办法:

  1. 利用虚拟环境或直接将urllib3版本降为1.25.11 (pip install urllib3==1.25.11)
  2.  修改代码
proxies = {
'http': 'http://127.0.0.1:7890',
'https': 'http://127.0.0.1:7890'  # https -> http
}
 
response = requests.post(url, headers=headers, json=params, proxies=proxies)

原因详情及更多办法请参考此链接,感谢这位兄弟。

更多推荐

Python请求错误“ check_hostname requires server_hostname”?