解决python2.x用urllib2证书验证错误, _create_unverified_context

本文解决了Python2.x中使用urllib2时出现的证书验证错误,包括AttributeError和URLError,并提供了详细的代码示例来禁用证书验证,适用于特定环境下的需求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

解决以下错误:

错误1:AttributeError: 'module' object has no attribute '_create_unverified_context',

错误2:URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:661)>

 

在代码中加入以下代码:

复制代码
 1 import ssl
 2 
 3 try:
 4     _create_unverified_https_context = ssl._create_unverified_context
 5 except AttributeError:
 6     # Legacy Python that doesn't verify HTTPS certificates by default
 7     pass
 8 else:
 9     # Handle target environment that doesn't support HTTPS verification
10     ssl._create_default_https_context = _create_unverified_https_context
复制代码

官方解释:

复制代码
This guidance is aimed primarily at system administrators that wish to adopt newer 
versions of Python that implement this PEP in legacy environments that do not yet
support certificate verification on HTTPS connections. For example, an administrator
may opt out by adding the monkeypatch above to sitecustomize.py in their Standard
Operating Environment for Python. Applications and libraries SHOULD NOT be making
this change process wide (except perhaps in response to a system administrator
controlled configuration setting). Particularly security sensitive applications should always provide an explicit
application defined SSL context rather than relying on the default behaviour
of the underlying Python implementation.
复制代码

 

Python访问https链接错误排查

 

1. 遇到这种错误有可能是机器无外网权限造成的。

  requests.exceptions.ConnectionError: HTTPSConnectionPool(host='', port=443): Max retries exceeded with url: /s/savecomtajax (Caused by NewConnectionError('<requests.packages.urllib3.connection.VerifiedHTTPSConnection object at 0x7f972f48bf90>: Failed to establish a new connection: [Errno 110] Connection timed out',))

2.  requests 发https 请求时,需要忽略证书的验证, 需要注意如下点。

requests.packages.urllib3.disable_warnings()
def request_ajax_url(login_url,login_body, headers):
    '''发送post请求数据'''
    req = requests.post(login_url, data=login_body, headers=headers, verify=False);
    return  req.text

  

posted on 2018-05-23 14:00 _潜行者 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/wangwei916797941/p/9076596.html

(base) PS D:\develop\Code\Code\Python\yolov5-master\yolov5-master> conda activate yolo (yolo) PS D:\develop\Code\Code\Python\yolov5-master\yolov5-master> python tied_detection_1.py Traceback (most recent call last): File "D:\develop\anaconda\envs\yolo\lib\site-packages\urllib3\connectionpool.py", line 466, in _make_request self._validate_conn(conn) File "D:\develop\anaconda\envs\yolo\lib\site-packages\urllib3\connectionpool.py", line 1095, in _validate_conn conn.connect() File "D:\develop\anaconda\envs\yolo\lib\site-packages\urllib3\connection.py", line 730, in connect sock_and_verified = _ssl_wrap_socket_and_match_hostname( File "D:\develop\anaconda\envs\yolo\lib\site-packages\urllib3\connection.py", line 909, in _ssl_wrap_socket_and_match_hostname ssl_sock = ssl_wrap_socket( File "D:\develop\anaconda\envs\yolo\lib\site-packages\urllib3\util\ssl_.py", line 469, in ssl_wrap_socket ssl_sock = _ssl_wrap_socket_impl(sock, context, tls_in_tls, server_hostname) File "D:\develop\anaconda\envs\yolo\lib\site-packages\urllib3\util\ssl_.py", line 513, in _ssl_wrap_socket_impl return ssl_context.wrap_socket(sock, server_hostname=server_hostname) File "D:\develop\anaconda\envs\yolo\lib\ssl.py", line 500, in wrap_socket return self.sslsocket_class._create( File "D:\develop\anaconda\envs\yolo\lib\ssl.py", line 1073, in _create self.do_handshake() File "D:\develop\anaconda\envs\yolo\lib\ssl.py", line 1342, in do_handshake self._sslobj.do_handshake() ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1149) During handling of the above exception, another exception occurred: Traceback (most recent call last): File "D:\develop\anaconda\envs\yolo\lib\site-packages\urllib3\connectionpool.py", line 789, in urlopen response = self._make_request( File "D:\develop\anaconda\envs\yolo\lib\site-packages\urllib3\connectionpool.py", line 490
最新发布
03-23
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值