小编dmi*_*iys的帖子

由于Python中的请求不安全URL而捕获SSLError?

我有一个包含几千个 URL 的列表,并注意到其中一个在传递SSLError到requests.get(). 下面是我尝试使用此类似问题中建议的解决方案以及使用“try & except”块捕获错误的失败尝试来解决此问题ssl.SSLError:

url = 'https://archyworldys.com/lidl-recalls-puff-pastry/'

session = requests.Session()
retry = Retry(connect=3, backoff_factor=0.5)
adapter = HTTPAdapter(max_retries=retry)
session.mount('http://', adapter)
session.mount('https://', adapter)

try:
    response = session.get(url,allow_redirects=False,verify=True)
except ssl.SSLError:
    pass
Run Code Online (Sandbox Code Playgroud)

最后返回的错误是:

SSLError: HTTPSConnectionPool(host='archyworldys.com', port=443): Max retries exceeded with url: /lidl-recalls-puff-pastry/ (Caused by SSLError(SSLError("bad handshake: Error([('SSL routines', 'ssl3_get_server_certificate', 'certificate verify failed')],)",),))
Run Code Online (Sandbox Code Playgroud)

当我在 Chrome 中打开该 URL 时,收到阻止该网页的“不安全”/“隐私错误”。但是,如果我尝试使用 HTTP 而不是 HTTPS 的 URL(例如“ http://archyworldys.com/lidl-recalls-puff-pastry/ ”),它在我的浏览器中工作得很好。根据这个问题,设置verify可以False解决问题,但我更喜欢找到更安全的解决方法。

虽然我知道一个简单的解决方案是从我的数据中删除该 URL,但我正在尝试找到一种解决方案,让我可以for loop …

python error-handling ssl python-requests

1
推荐指数
1
解决办法
5073
查看次数

标签 统计

error-handling ×1

python ×1

python-requests ×1

ssl ×1