首页 > 解决方案 > 尝试访问 HTTPS 站点时,Python 请求模块无法与代理一起使用

问题描述

我正在尝试使用高级代理 IP 连接到使用请求模块的网站,但它不适用于“https”网站。

import requests
from requests.auth import HTTPProxyAuth
auth=HTTPProxyAuth('username','password')
proxies={
'http':'http://xxx.xxx.x.xxx:8080/',
'https':'http://xxx.xxx.x.xxx:8080/'
}
print(requests.get('https://www.google.com',proxies=proxies,auth=auth).status_code)

抛出以下错误:

raise ProxyError(e, request=request)
requests.exceptions.ProxyError: HTTPSConnectionPool(host='www.google.com',port=443): Max retries exceeded with url: / (Caused by ProxyError('Cannot connect to proxy.', OSError('Tunnel connection failed: 407 Proxy Authentication Required')))

注意:我尝试使用各种猴子补丁,这些补丁甚至从 7 年前就建议进行 Digest Auth,但它们似乎都不起作用。

编辑1:

我没有使用 http://username@password:ip:port/ 格式来使用代理,因为我的用户名包含“@”,这会导致解析错误。

提前致谢 :)

标签: pythonpython-3.xhttpsproxypython-requests

解决方案


我想你可以用这个

http://username@password:ip:port/

您提到您的密码包含特殊字符

'@' 可以替换为 '%40'

例如 ,

'Pass@word' 可以替换为 'Pass%40word'

如果您的密码中有更多特殊字符,以下数据可能会有所帮助

'@' = '%40'
'%' ='%25'
'#' = '%23'
'@' = '%40'
':' = '%3a'
';' = '%3b'
'?' = '%3f'
'$' = '%24'
'!' = '%21'
'/' = '%2f'
'\' = '%27'
'"' = '%22'

推荐阅读