打开fiddler,使用requests请求时报错requests.exceptions.SSLError: HTTPSConnectionPool

问题描述:

使用fiddler进行抓包后,fiddler不关闭,使用pycharm进行url访问,报错,如下:

Traceback (most recent call last):
  File "F:\project\venv\lib\site-packages\urllib3\connectionpool.py", line 594, in urlopen
    self._prepare_proxy(conn)
  File "F:\project\venv\lib\site-packages\urllib3\connectionpool.py", line 805, in _prepare_proxy
    conn.connect()
  File "F:\project\venv\lib\site-packages\urllib3\connection.py", line 344, in connect
    ssl_context=context)
  File "F:\project\venv\lib\site-packages\urllib3\util\ssl_.py", line 344, in ssl_wrap_socket
    return context.wrap_socket(sock, server_hostname=server_hostname)
  File "G:\anaconda\lib\ssl.py", line 407, in wrap_socket
    _context=self, _session=session)
  File "G:\anaconda\lib\ssl.py", line 814, in __init__
    self.do_handshake()
  File "G:\anaconda\lib\ssl.py", line 1068, in do_handshake
    self._sslobj.do_handshake()
  File "G:\anaconda\lib\ssl.py", line 689, in do_handshake
    self._sslobj.do_handshake()
ssl.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:777)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "F:\project\venv\lib\site-packages\requests\adapters.py", line 449, in send
    timeout=timeout
  File "F:\project\venv\lib\site-packages\urllib3\connectionpool.py", line 638, in urlopen
    _stacktrace=sys.exc_info()[2])
  File "F:\project\venv\lib\site-packages\urllib3\util\retry.py", line 398, in increment
    raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='www.baidu.com', port=443): Max retries exceeded with url: / (Caused by SSLError(SSLError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:777)'),))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "F:/project/python_test3.py", line 2, in 
    res = requests.get("https://www.baidu.com/")
  File "F:\project\venv\lib\site-packages\requests\api.py", line 75, in get
    return request('get', url, params=params, **kwargs)
  File "F:\project\venv\lib\site-packages\requests\api.py", line 60, in request
    return session.request(method=method, url=url, **kwargs)
  File "F:\project\venv\lib\site-packages\requests\sessions.py", line 533, in request
    resp = self.send(prep, **send_kwargs)
  File "F:\project\venv\lib\site-packages\requests\sessions.py", line 646, in send
    r = adapter.send(request, **kwargs)
  File "F:\project\venv\lib\site-packages\requests\adapters.py", line 514, in send
    raise SSLError(e, request=request)
requests.exceptions.SSLError: HTTPSConnectionPool(host='www.baidu.com', port=443): Max retries exceeded with url: / (Caused by SSLError(SSLError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:777)'),))

代码如下:

import requests
res = requests.get("https://www.baidu.com/")
print(res.text)

当我将url中的https改为http的时候,即使开着fiddler也不会报错

原因分析:

ssl验证失败

解决方案:

在使用requests发送请求的时候加上verify这个参数并设置为False

正确代码如下

import requests
res = requests.get("https://www.baidu.com/",verify=False)
print(res.text)

再次执行,执行结果如下:

F:\project\venv\Scripts\python.exe F:/project/python_test3.py
F:\project\venv\lib\site-packages\urllib3\connectionpool.py:847: InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings
  InsecureRequestWarning)

 ç¾åº¦ä¸ä¸ï¼ä½ å°±ç¥é  

å³äºç¾åº¦ About Baidu

©2017 Baidu ä½¿ç¨ç¾åº¦åå¿è¯»  æè§å馠京ICPè¯030173å· 

Process finished with exit code 0

 结果出现警告,Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings InsecureRequestWarning)

想去掉红框内的内容还需要添加如下代码

requests.packages.urllib3.disable_warnings()

正确代码:

import requests
requests.packages.urllib3.disable_warnings()
res = requests.get("https://www.baidu.com/",verify=False)
print(res.text)

执行结果如下,执行成功:

F:\project\venv\Scripts\python.exe F:/project/python_test3.py

 ç¾åº¦ä¸ä¸ï¼ä½ å°±ç¥é  

å³äºç¾åº¦ About Baidu

©2017 Baidu ä½¿ç¨ç¾åº¦åå¿è¯»  æè§å馠京ICPè¯030173å· 

Process finished with exit code 0

 

感谢大大,参考网址:

https://blog.csdn.net/win_turn/article/details/77142100

https://blog.csdn.net/weixin_39461487/article/details/81132922

你可能感兴趣的:(错误整理,警告,爬虫问题,fiddler)