小编Arp*_*ire的帖子

通过SSLError的异常处理请求

这是我检查链接的功能。但是,当链接为假时,它将引发错误。例如,它适用于twitter.com,但不适用于twitt.com。

class Quality_Check:
    def check_broken_link(self,data):
        url= requests.head(data)

        try:
            if url.status_code==200 or url.status_code==302 or url.status_code==301:
                return True
        except requests.exceptions.SSLError as e:
            return False

qc=Quality_Check()
print(qc.check_broken_link('https://twitte.com'))
Run Code Online (Sandbox Code Playgroud)

当我尝试通过此方法处理异常时,显示以下错误:

Traceback (most recent call last):
      ssl.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:749)
Run Code Online (Sandbox Code Playgroud)

在处理上述异常期间,发生了另一个异常:

raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='twitte.com', 
port=443): Max retries exceeded with url: / (Caused by SSLError(SSLError(1, 
'[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:749)'),))
Run Code Online (Sandbox Code Playgroud)

还有一个也出现了

requests.exceptions.SSLError: HTTPSConnectionPool(host='twitte.com', port=443): Max retries exceeded with url: / (Caused by SSLError(SSLError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed …
Run Code Online (Sandbox Code Playgroud)

python error-handling

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

标签 统计

error-handling ×1

python ×1