python requests SSL认证不过导致失败
request请求https报错:
>>> requests.get('https://33.157.0.114:8321/encryptDogService/dogInfo/base') Traceback (most recent call last): File "C:\ProgramData\Anaconda3\lib\site-packages\urllib3\connectionpool.py", line 699, in urlopen httplib_response = self._make_request( File "C:\ProgramData\Anaconda3\lib\site-packages\urllib3\connectionpool.py", line 382, in _make_request self._validate_conn(conn) File "C:\ProgramData\Anaconda3\lib\site-packages\urllib3\connectionpool.py", line 1010, in _validate_conn conn.connect() File "C:\ProgramData\Anaconda3\lib\site-packages\urllib3\connection.py", line 411, in connect self.sock = ssl_wrap_socket( File "C:\ProgramData\Anaconda3\lib\site-packages\urllib3\util\ssl_.py", line 432, in ssl_wrap_socket ssl_sock = _ssl_wrap_socket_impl(sock, context, tls_in_tls) File "C:\ProgramData\Anaconda3\lib\site-packages\urllib3\util\ssl_.py", line 474, in _ssl_wrap_socket_impl return ssl_context.wrap_socket(sock) File "C:\ProgramData\Anaconda3\lib\ssl.py", line 500, in wrap_socket return self.sslsocket_class._create( File "C:\ProgramData\Anaconda3\lib\ssl.py", line 1040, in _create self.do_handshake() File "C:\ProgramData\Anaconda3\lib\ssl.py", line 1309, in do_handshake self._sslobj.do_handshake() ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate in certificate chain (_ssl.c:1125) During handling of the above exception, another exception occurred: Traceback (most recent call last): File "C:\ProgramData\Anaconda3\lib\site-packages\requests\adapters.py", line 439, in send resp = conn.urlopen( File "C:\ProgramData\Anaconda3\lib\site-packages\urllib3\connectionpool.py", line 755, in urlopen retries = retries.increment( File "C:\ProgramData\Anaconda3\lib\site-packages\urllib3\util\retry.py", line 574, in increment raise MaxRetryError(_pool, url, error or ResponseError(cause)) urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='33.157.0.114', port=8321): Max retries exceeded with url: /encryptDogService/dogInfo/base (Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate in certificate chain (_ssl.c:1125)'))) During handling of the above exception, another exception occurred: Traceback (most recent call last): File "<stdin>", line 1, in <module> File "C:\ProgramData\Anaconda3\lib\site-packages\requests\api.py", line 75, in get return request('get', url, params=params, **kwargs) File "C:\ProgramData\Anaconda3\lib\site-packages\requests\api.py", line 61, in request return session.request(method=method, url=url, **kwargs) File "C:\ProgramData\Anaconda3\lib\site-packages\requests\sessions.py", line 542, in request resp = self.send(prep, **send_kwargs) File "C:\ProgramData\Anaconda3\lib\site-packages\requests\sessions.py", line 655, in send r = adapter.send(request, **kwargs) File "C:\ProgramData\Anaconda3\lib\site-packages\requests\adapters.py", line 514, in send raise SSLError(e, request=request) requests.exceptions.SSLError: HTTPSConnectionPool(host='33.157.0.114', port=8321): Max retries exceeded with url: /encryptDogService/dogInfo/base (Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate in certificate chain (_ssl.c:1125)')))
解决方法:
请求时加上
verify=False
>>> requests.get('https://33.157.0.114:8321/encryptDogService/dogInfo/base',verify=False).text '{"currentTime":"20220104T014236Z","expireDate":"2022-06-17","totalCount":1,"version":"VSL_3.0_20200320_XX"}\n'
补充:
加完以后不报错了,但是会在命令行里弹警告
InsecureRequestWarning: unverified HTTPS request is being made to host
影响观感,需要加上:
import urllib3 urllib3.disable_warnings()