简体   繁体   English

由于Python中的请求的网址不安全而捕获SSLError吗?

[英]Catching SSLError due to unsecure URL with requests in Python?

I have a list of a few thousand URLs and noticed one of them is throwing as SSLError when passed into requests.get() . 我有一个数千个URL的列表,并且注意到其中一个URL传递给requests.get()时抛出SSLError Below is my attempt to work around this using both a solution suggested in this similar question as well as a failed attempt to catch the error with a "try & except" block using ssl.SSLError : 下面是我尝试使用类似问题中建议解决方案来解决此问题 ,以及使用ssl.SSLError使用“ try&except”块捕获错误的失败尝试:

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

The error returned at the very end is: 最后返回的错误是:

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')],)",),))

When I opened the URL in Chrome, I get a "Not Secure" / "Privacy Error" that blocks the webpage. 当我在Chrome中打开URL时,出现阻止网页的“不安全” /“隐私错误”。 However, if I try the URL with HTTP instead of HTTPS (eg ' http://archyworldys.com/lidl-recalls-puff-pastry/ ') it works just fine in my browser. 但是,如果我尝试使用HTTP而不是HTTPS的URL(例如“ http://archyworldys.com/lidl-recalls-puff-pastry/ ”),则在我的浏览器中可以正常使用。 Per this question , setting verify to False solves the problem, but I prefer to find a more secure work-around. 对于这个问题 ,将verify设置为False可以解决此问题,但是我更喜欢找到一个更安全的解决方法。

While I understand a simple solution would be to remove the URL from my data, I'm trying to find a solution that let's me proceed (eg if in a for loop ) by simply skipping this bad URL and moving on the next one. 虽然我知道一个简单的解决方案是从数据中删除该URL,但我正在尝试找到一种解决方案,让我继续(例如,如果在for loop )跳过该错误URL并继续下一个。

The error I get when running your code is: 运行代码时出现的错误是:

requests.exceptions.SSLError: requests.exceptions.SSLError:
[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:645) [SSL:CERTIFICATE_VERIFY_FAILED]证书验证失败(_ssl.c:645)

Based on this one needs to catch requests.exceptions.SSLError and not ssl.SSLError , ie: 在此基础上一个需要捕获requests.exceptions.SSLError而不是ssl.SSLError ,即:

try:
    response = session.get(url,allow_redirects=False,verify=True)
except requests.exceptions.SSLError:
    pass

While it looks like the error you get is different this is probably due the code you show being not exactly the code you are running. 虽然您看到的错误有所不同,但这可能是由于您显示的代码与运行的代码不完全相同。 Anyway, look at the exact error message you get and figure out from this which exception exactly to catch. 无论如何,请查看您获得的确切错误消息,并从中找出确切捕获的异常。 You might also try to catch a more general exception like this and by doing this get the exact Exception class you need to catch: 您还可以尝试捕获这样的更通用的异常,并通过此操作获取需要捕获的确切Exception类:

try:
    response = session.get(url,allow_redirects=False,verify=True)
except Exception as x:
    print(type(x),x)
    pass

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM