python - 尝试访问 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/ 格式来使用代理,因为我的用户名包含“@”,这会导致解析错误。
提前致谢 :)
解决方案
我想你可以用这个
http://username@password:ip:port/
您提到您的密码包含特殊字符
'@' 可以替换为 '%40'
例如 ,
'Pass@word' 可以替换为 'Pass%40word'
如果您的密码中有更多特殊字符,以下数据可能会有所帮助
'@' = '%40'
'%' ='%25'
'#' = '%23'
'@' = '%40'
':' = '%3a'
';' = '%3b'
'?' = '%3f'
'$' = '%24'
'!' = '%21'
'/' = '%2f'
'\' = '%27'
'"' = '%22'
推荐阅读
- xslt - Remove whitespace left by element removal using XSLT
- python - 如何用 BeautifulSoup 解析字典结构数据?
- java - 有没有办法停止从基类运行测试,但仍然让这些测试从它的 Java 子类运行?
- r - 南极洲形状的 tmap 问题“if (any(zeros)) {”中的错误
- r - R unscale and back transform plot axis or use axis from original data column
- r - 如何在ggplot2中将重要变量相互隔离
- typescript - 在从组合 API 创建的生命周期钩子中使用它
- sql - SQL Server - 动态平均值
- javascript - 如果存在某个参数,我如何编写一个需要属性的自定义错误类?
- amazon-s3 - 在另一个账户中跨账户访问 AWS Glue 的 S3