During handling of the above exception, another exception occurred
字面意思为:在处理上述异常的过程中,发生了另一个异常。简单理解就是,程序执行——>异常——>异常处理——>又引发异常——>又异常处理…此时,在报错信息之间就会出现上面一行英文。
代码示例
# -*- coding:utf-8 -*-
# date:
# info: during_handling.py
# Python 3.8.8
import requests
class DuringHandle(object):
def __init__(self):
self.HEADERS = {
'Accept': 'application/json, text/javascript, */*; q=0.01',
'Accept-Encoding': 'gzip, deflate, br',
'Accept-Language': 'zh-CN,zh;q=0.9',
'Connection': 'keep-alive',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36',
'X-Requested-With': 'XMLHttpRequest',
}
self.code = 'Nhf876Y8mk1sdi2lf'
self.RS = requests.Session()
def get_session_id(self):
u = 'https://cm.lanjing.com/account/sso/?next=/asset/server/list&code={}'.format(self.code)
req = self.RS.get(url=u, headers=self.HEADERS)
return req.cookies['sessionid']
def main_run(self):
session_id = self.get_session_id()
print(session_id)
if __name__ == '__main__':
dh = DuringHandle()
dh.main_run()
运行代码
由于 req = self.RS.get(url=u, headers=self.HEADERS) 缺少参数 verify=False ,在执行上述代码,抛出如下异常
Traceback (most recent call last):
File "E:\pythonProject\proCmp\venv\lib\site-packages\urllib3\connectionpool.py", line 670, in urlopen
httplib_response = self._make_request(
File "E:\pythonProject\proCmp\venv\lib\site-packages\urllib3\connectionpool.py", line 381, in _make_request
self._validate_conn(conn)
File "E:\pythonProject\proCmp\venv\lib\site-packages\urllib3\connectionpool.py", line 978, in _validate_conn
conn.connect()
File "E:\pythonProject\proCmp\venv\lib\site-packages\urllib3\connection.py", line 362, in connect
self.sock = ssl_wrap_socket(
File "E:\pythonProject\proCmp\venv\lib\site-packages\urllib3\util\ssl_.py", line 386, in ssl_wrap_socket
return context.wrap_socket(sock, server_hostname=server_hostname)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38\lib\ssl.py", line 500, in wrap_socket
return self.sslsocket_class._create(
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38\lib\ssl.py", line 1040, in _create
self.do_handshake()
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38\lib\ssl.py", line 1309, in do_handshake
self._sslobj.do_handshake()
ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: certificate has expired (_ssl.c:1125)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "E:\pythonProject\proCmp\venv\lib\site-packages\requests\adapters.py", line 439, in send
resp = conn.urlopen(
File "E:\pythonProject\proCmp\venv\lib\site-packages\urllib3\connectionpool.py", line 726, in urlopen
retries = retries.increment(
File "E:\pythonProject\proCmp\venv\lib\site-packages\urllib3\util\retry.py", line 446, in increment
raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='cm.lanjing.com', port=443): Max retries exceeded with url: /account/sso/?next=/asset/server/list&code=Nhf876Y8mk1sdi2lf (Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: certificate has expired (_ssl.c:1125)')))
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "E:/pythonProject/proCmp/py_script/during_handling.py", line 215, in <module>
dh.main_run()
File "E:/pythonProject/proCmp/py_script/during_handling.py", line 162, in main_run
session_id = self.get_session_id()
File "E:/pythonProject/proCmp/py_script/during_handling.py", line 65, in get_session_id
req = self.RS.get(url=u, headers=self.HEADERS)
File "E:\pythonProject\proCmp\venv\lib\site-packages\requests\sessions.py", line 543, in get
return self.request('GET', url, **kwargs)
File "E:\pythonProject\proCmp\venv\lib\site-packages\requests\sessions.py", line 530, in request
resp = self.send(prep, **send_kwargs)
File "E:\pythonProject\proCmp\venv\lib\site-packages\requests\sessions.py", line 643, in send
r = adapter.send(request, **kwargs)
File "E:\pythonProject\proCmp\venv\lib\site-packages\requests\adapters.py", line 514, in send
raise SSLError(e, request=request)
requests.exceptions.SSLError: HTTPSConnectionPool(host='cm.lanjing.com', port=443): Max retries exceeded with url: /account/sso/?next=/asset/server/list&code=Nhf876Y8mk1sdi2lf (Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: certificate has expired (_ssl.c:1125)')))
Process finished with exit code 1
报错解读
第一个 Traceback 触发了 ssl.SSLCertVerificationError 报错,且下面出现 During handling of the above exception, another exception occurred ,继续看第二个 Traceback,触发了 urllib3.exceptions.MaxRetryError,且下面也出现了 During handling of the above exception, another exception occurred,继续看第三个 Traceback,触发了 requests.exceptions.SSLError ,且只有此处的异常是由自编程的代码导致了异常,第一、二个均是Python的库代码的异常。文章来源:https://www.toymoban.com/news/detail-640181.html
尝试修改代码
# 省略部分代码
# return req.cookies['sessionid']
try:
req = self.RS.get(url=u, headers=self.HEADERS)
return req.cookies['sessionid']
except requests.exceptions.SSLError as e:
print('requests.exceptions.SSLError:{}'.format(e))
return
# 省略部分代码
运行代码
运行代码后发现,程序完整运行,自定义输出异常内容文章来源地址https://www.toymoban.com/news/detail-640181.html
requests.exceptions.SSLError:HTTPSConnectionPool(host='cm.lanjing.com', port=443): Max retries exceeded with url: /account/sso/?next=/asset/server/list&code=Nhf876Y8mk1sdi2lf (Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: certificate has expired (_ssl.c:1125)')))
到了这里,关于During handling of the above exception, another exception occurred 处理的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!