解决以下错误:

错误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

  

解决python2.x用urllib2证书验证错误, _create_unverified_context的更多相关文章

  1. Axis 1 https(SSL) client 证书验证错误ValidatorException workaround

    Axis 1.x 编写的client在测试https的webservice的时候, 由于client 代码建立SSL连接的时候没有对truststore进行设置,在与https部署的webservic ...

  2. Xcode中使用svn时,报证书验证错误Error validating server certificate for

    转:http://blog.csdn.net/yhawaii/article/details/7511141 今天使用Xcode自带的svn客户端时,总是连接不上服务器,报如下错误: Error va ...

  3. HTTPS请求 SSL证书验证

    import urllib2 url = "https://www.12306.cn/mormhweb/" headers = {"User-Agent": & ...

  4. 调用微信退款接口时,证书验证出现System.Security.Cryptography.CryptographicException: 出现了内部错误 解决办法

    1.证书密码不正确,微信证书密码就是商户号 解决办法:请检查证书密码是不是和商户号一致 2.IIS设置错误,未加载用户配置文件 解决办法:找到网站使用的应用程序池-->右击-->高级设置- ...

  5. 解决https证书验证不通过的问题

    1.报错信息 java.security.cert.CertificateException: No name matching api.weibo.com found; nested excepti ...

  6. 解决Python2.7的UnicodeEncodeError:'ascii' codec can't encode characters in position 0-78: ordinal not in range(128)异常错误

    解决Python2.7的UnicodeEncodeError: 'ascii' codec can't encode异常错误 大家都知道,在使用python进行网络爬虫时,最头疼的就是转码问题,下面是 ...

  7. Win10远程桌面 出现 身份验证错误,要求的函数不受支持,这可能是由于CredSSP加密Oracle修正 解决方法

    升级至win10 最新版本18362,远程桌面连接Window Server时报错信息如下: 出现身份验证错误,要求的函数不正确,这可能是由于CredSSP加密Oracle修正. 解决方法: 运行 g ...

  8. [Java] 绕过证书验证调 HTTPS 接口时报 “SSLHandshakeException: DHPublicKey does not comply to algorithm constraints”的解决办法

    作者: zyl910 一.缘由 最近有在对接一个无证书的HTTPS接口时,总是收到"SSLHandshakeException: DHPublicKey does not comply to ...

  9. Win10远程桌面出现 身份验证错误,要求的函数不受支持,这可能是由于CredSSP加密Oracle修正 解决方法

    升级至win10 最新版本10.0.17134,远程桌面连接Window Server时报错信息如下: 出现身份验证错误,要求的函数不正确,这可能是由于CredSSP加密Oracle修正. 解决方法: ...

随机推荐

  1. mybatis ---- 实现数据的增删改查

    前面介绍了接口方式的编程,需要注意的是:在book.xml文件中,<mapper namespace="com.mybatis.dao.IBookDao"> ,命名空间 ...

  2. tvtk管线技术、数据集与数据加载

    管线技术也称流水线技术(Pipeline)每个对象只实现相对简单的任务,整个管线进行复杂的可视化处理在tvtk中分为可视化管线和图形管线 可视化管线(Visualization Pipeline):将 ...

  3. GitHub Desktop 如何创建本地仓库,上传代码,删除仓库

    1.创建本地仓库 2.打开本地仓库,将要上传的文件放到本地仓库. 3.ctrl+p push仓库或者菜单栏Repository下push也可以用右上角的publish respository 4.左边 ...

  4. 9.3.3 scrapy 框架

    scrapy是一个非常好用的Web爬虫框架,非常适合抓取Web站点从网页中提取结构化的数据,并且支持自定义的需求.在使用scrapy爬取网页数据时,除了熟悉HTML标签,还需要了解目标网页的数据组织结 ...

  5. Python_@修饰器(装饰器)的理解

    装饰器本质上是一个Python函数,它可以让其他函数在不需要做任何代码变动的前提下增加额外功能. 装饰器的作用就是为已经存在的对象添加额外的功能. def funA(fun): print (fun( ...

  6. JVM笔记9-Class类文件结构

    1.Class类文件结构  Class 文件是一组以 8 位字节为基础单位的二进制流,各个数据项目严格按照顺序紧凑地排列在 Class 文件之中,中间没有添加任何分隔符,这使得整个 Class 文件中 ...

  7. 关于HTTP请求、Ajax请求,请求的同步和异步

    使用了很长时间的Ajax请求了,一直都是在以异步的方式在使用.昨天听了一个讲座涉及到apache server,偶然想到了这Ajax请求和HTTP请求的一些区别和联系,就在网上好好搜了一顿,把搜到的结 ...

  8. Beta 冲刺day 6

    1.昨天的困难,今天解决的进度,以及明天要做的事情 昨天的困难:在导入导出方面遇到了困难,导出的文件不能直接导入. 今天解决的进度:完成了登录页面的背景设计,并再次测试了整个系统的功能. 明天要做的事 ...

  9. jasperReport Studio java报表设计(详细)

    一.环境搭建 在spring-mvc.xml加入 <!-- jasperReports--><import resource="classpath*:spring-mvc- ...

  10. Python--Seaborn绘图和可视化--基本语法

    博客地址:http://www.cnblogs.com/yudanqu/ Seaborn是对matplotlib的extend,是一个数据可视化库,提供更高级的API封装,在应用中更加的方便灵活.下面 ...