前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >Python 获取 NCBI 基因名 SSL 证书异常?

Python 获取 NCBI 基因名 SSL 证书异常?

作者头像
章鱼猫先生
发布于 2021-10-15 03:42:39
发布于 2021-10-15 03:42:39
92300
代码可运行
举报
文章被收录于专栏:BioIT爱好者BioIT爱好者
运行总次数:0
代码可运行

源自于前几天对一批转录本的批量化操作。

即想要通过 Python 在线获取某个转录本对应的基因 symbol 时,发现出现 SSL 无法获取本地证书:unable to get local issuer certificate (_ssl.c:1056)

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
>>> from Bio import SeqIO
>>> from Bio import Entrez
>>> Entrez.email = "A.N.Other@example.com"
>>> records = SeqIO.parse(Entrez.efetch(id='NM_001009537.4', db="nucleotide", rettype="gb", retmode="text"), "gb")
Traceback (most recent call last):
  File "/Bioinfo/Pipeline/SoftWare/Python-3.7.3/lib/python3.7/urllib/request.py", line 1317, in do_open
    encode_chunked=req.has_header('Transfer-encoding'))
  File "/Bioinfo/Pipeline/SoftWare/Python-3.7.3/lib/python3.7/http/client.py", line 1229, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "/Bioinfo/Pipeline/SoftWare/Python-3.7.3/lib/python3.7/http/client.py", line 1275, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "/Bioinfo/Pipeline/SoftWare/Python-3.7.3/lib/python3.7/http/client.py", line 1224, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "/Bioinfo/Pipeline/SoftWare/Python-3.7.3/lib/python3.7/http/client.py", line 1016, in _send_output
    self.send(msg)
  File "/Bioinfo/Pipeline/SoftWare/Python-3.7.3/lib/python3.7/http/client.py", line 956, in send
    self.connect()
  File "/Bioinfo/Pipeline/SoftWare/Python-3.7.3/lib/python3.7/http/client.py", line 1392, in connect
    server_hostname=server_hostname)
  File "/Bioinfo/Pipeline/SoftWare/Python-3.7.3/lib/python3.7/ssl.py", line 412, in wrap_socket
    session=session
  File "/Bioinfo/Pipeline/SoftWare/Python-3.7.3/lib/python3.7/ssl.py", line 853, in _create
    self.do_handshake()
  File "/Bioinfo/Pipeline/SoftWare/Python-3.7.3/lib/python3.7/ssl.py", line 1117, in do_handshake
    self._sslobj.do_handshake()
ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1056)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Bioinfo/Pipeline/SoftWare/Python-3.7.3/lib/python3.7/site-packages/Bio/Entrez/__init__.py", line 184, in efetch
    return _open(cgi, variables, post=post)
  File "/Bioinfo/Pipeline/SoftWare/Python-3.7.3/lib/python3.7/site-packages/Bio/Entrez/__init__.py", line 543, in _open
    handle = _urlopen(cgi)
  File "/Bioinfo/Pipeline/SoftWare/Python-3.7.3/lib/python3.7/urllib/request.py", line 222, in urlopen
    return opener.open(url, data, timeout)
  File "/Bioinfo/Pipeline/SoftWare/Python-3.7.3/lib/python3.7/urllib/request.py", line 525, in open
    response = self._open(req, data)
  File "/Bioinfo/Pipeline/SoftWare/Python-3.7.3/lib/python3.7/urllib/request.py", line 543, in _open
    '_open', req)
  File "/Bioinfo/Pipeline/SoftWare/Python-3.7.3/lib/python3.7/urllib/request.py", line 503, in _call_chain
    result = func(*args)
  File "/Bioinfo/Pipeline/SoftWare/Python-3.7.3/lib/python3.7/urllib/request.py", line 1360, in https_open
    context=self._context, check_hostname=self._check_hostname)
  File "/Bioinfo/Pipeline/SoftWare/Python-3.7.3/lib/python3.7/urllib/request.py", line 1319, in do_open
    raise URLError(err)
urllib.error.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1056)>

根据网络上的解析,当使用 urllib.urlopen 打开一个 https 链接时,会验证一次 SSL 证书。而当目标网站使用的是自签名的证书时就会抛出如下异常:

ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1056)

下面是个人简单总结的 3 种常用解决方法。

方法一,全局取消证书验证

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
>>> import ssl
>>> ssl._create_default_https_context = ssl._create_unverified_context

方法二,指定位置安装证书

  1. 查看证书默认位置。由于在 openssl_cafile 中指定的 ca 文件(cert.pem)不存在,所以导致上面的错误。
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
>>> import ssl
>>> print(ssl.get_default_verify_paths())
DefaultVerifyPaths(cafile=None, capath='/Bioinfo/Pipeline/SoftWare/LibDependence/openssl-1.1.1/ssl/certs', openssl_cafile_env='SSL_CERT_FILE', openssl_cafile='/Bioinfo/Pipeline/SoftWare/LibDependence/openssl-1.1.1/ssl/cert.pem', openssl_capath_env='SSL_CERT_DIR', openssl_capath='/Bioinfo/Pipeline/SoftWare/LibDependence/openssl-1.1.1/ssl/certs')
  1. 下载 ca 文件,将下载的 ca 文件放到 openssl_cafile 指定位置。注意,如果放到 openssl_capath 目录下还会出现类似的问题,一定要放到 openssl_cafile 指定的位置。
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
$ cd /Bioinfo/Pipeline/SoftWare/LibDependence/openssl-1.1.1/ssl
$ wget http://curl.haxx.se/ca/cacert.pem -O cert.pem --no-check-certificate

方法三,设置环境变量

也可以参考 stackoverflow 上的做法,通过修改下面环境变量的方式解决(亲测可行)。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
export SSL_CERT_FILE=/usr/local/etc/openssl/cert.pem
export REQUESTS_CA_BUNDLE=/usr/local/etc/openssl/cert.pem

最后,问题解决。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
>>> from Bio import SeqIO
>>> from Bio import Entrez
>>> Entrez.email = "A.N.Other@example.com"
>>> records = SeqIO.parse(Entrez.efetch(id='NM_001009537.4', db="nucleotide", rettype="gb", retmode="text"), "gb")
>>> for record in records:
...     for feature in record.features:
...         if feature.type == "gene":
...             print(feature.qualifiers['gene'])
...
['Zfp799']

END

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2021-09-27,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 BioIT爱好者 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
【错】urllib.error.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED]
http://stackoverflow.com/questions/27835619/ssl-certificate-verify-failed-error
静默加载
2020/05/29
1.6K0
Python爬虫 带你一键爬取王者荣耀英雄皮肤壁纸
王者荣耀这款手游,想必大家都玩过或听过,游戏里英雄有各式各样的皮肤,制作得很精美,有些拿来做电脑壁纸它不香吗。本文带你利用Python爬虫一键下载王者荣耀英雄皮肤壁纸。
叶庭云
2020/10/26
1.5K0
Python爬虫  带你一键爬取王者荣耀英雄皮肤壁纸
[820]fake_useragent.errors.FakeUserAgentError: Maximum amount of retries reached
fake_useragent中存储的UserAgent列表发生了变动,而本地UserAgent的列表未更新所导致的,在更新fake_useragent后报错就消失了。
周小董
2020/06/01
2.2K0
Error Downloading MNIST解决方案
下载mnist-original.mat并且保存到 ~/scikit_learn_data/mldata/(scikit data home dir)下面即可
zeekling
2022/06/17
3660
Python Requests:prox
推荐阅读:https://blog.csdn.net/wangzuxi/article/details/40377467  
py3study
2020/01/07
1.4K0
requests模块报错:Use body.encode('utf-8') if you want to send it encoded in UTF-8.
在做 企业向微信用户个人付款  功能时,调用第三方sdk,在 进行 requests 的post请求时,
用户1558882
2018/10/11
2.3K0
开源项目 requests 的 stars 为啥比 python 还多 3.7k?
结合上一篇文章《一次算法读图超时引起的urllib3源码分析》,我们学习了 urllib3 的基本语法、常见姿势和请求管理模式,以及PoolManager、HTTPConnectionPool、HTTPConnection等模块部分源码。对于学习 Python 的小伙伴来说,urllib3 强大的功能几乎能实现所有 HTTP 请求场景,但这就足够了吗?
程序员荒生
2022/03/15
7840
开源项目 requests 的 stars 为啥比 python 还多 3.7k?
CentOS7 升级 python3 过
(<http://blog.csdn.net/qq_25560423/article/details/62055497>;)
py3study
2020/01/06
1.2K0
Centos下升级Python
另一篇文章 Centos7安装Python3.7(兼容Python2.7)https://blog.51cto.com/leyex/2163465
py3study
2020/01/07
2K1
我为什么不建议你使用Python3.7.3?
之前使用Python的环境一直是Python3.7.3的,一直使用的很正常,没有什么毛病,直到最近做一个图片下载器的时候发现了问题。
云爬虫技术研究笔记
2019/11/05
2.2K0
我为什么不建议你使用Python3.7.3?
探索eventlet通信机制
该方法对某些系统模块进行全局打补丁,使其对Greenthread友好。关键字参数用于指定哪些模块需要打补丁,如果未提供关键字参数,则会对所有默认的模块(如代码所示)打补丁,例如: monkey_patch(socket = True,select = True) 仅对socket和select模块打补丁。大多数参数都是对同名的单个模块进行打补丁,比如操作系统,时间,选择。但是socket例外,它也会对ssl模块(如果存在)打补丁,thread用于对threading、thread、Queue打补丁。说明:多次调用monkey_patch是安全的。
tunsuy
2022/10/27
6000
使用 openssl 生成证书(含openssl详解)
openssl 是目前最流行的 SSL 密码库工具,其提供了一个通用、健壮、功能完备的工具套件,用以支持SSL/TLS 协议的实现。 官网:https://www.openssl.org/source/
菲宇
2019/06/12
17K0
使用 openssl 生成证书(含openssl详解)
Saltstack_使用指南18_API
1. 主机规划 salt 版本 1 [root@salt100 ~]# salt --version 2 salt 2018.3.3 (Oxygen) 3 [root@salt100 ~]# salt
踏歌行
2020/10/15
6980
Saltstack_使用指南18_API
python使用requests时报错requests.exceptions.SSLError: HTTPSConnectionPool
分析:由于报错SSL证书验证失败,所以这次的访问应该是https协议.但是我们明明使用的是http,所以,猜测访问该网站后,被重定向到了https://www.baidu.com/
用户1214487
2018/08/01
5.6K0
python使用requests时报错requests.exceptions.SSLError: HTTPSConnectionPool
编译Python3.7并配置ssl库为LibreSSL
下载源码,手动编译:./configure --prefix=/opt/python/,安装: make && make install。
the5fire
2019/03/01
4.2K0
OpenSSL 常用命令
雨落秋垣
2024/10/30
1300
教师妹学python之十:共享代码
PyPi 是 Python Package Index 的首字母简写,其实表示的是 Python 的 Packag 索引,这个也是 Python 的官方索引。
互联网金融打杂
2022/08/01
4430
教师妹学python之十:共享代码
centos7 install python3.7 with problem and how to fix it.
<!-- p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #000000; background-color: #ffffff} span.s1 {font-variant-ligatures: no-common-ligatures} -->
超蛋lhy
2018/08/31
6770
centos7 install python3.7 with problem and how to fix it.
npc_gzip笔记 - plus studio
npc_gzip 的论文名叫做 "Low-Resource" Text Classification: A Parameter-Free Classification Method with Compressors ,意为不需要参数,使用压缩器的文本分类方法。论文的代码也只有仅仅的十四行,就在部分数据集上取得了超越 bert 的效果。
plus sign
2024/02/29
1610
npc_gzip笔记 - plus studio
在Linux下如何根据域名自签发OpenSSL证书与常用证书转换 修改openssl.cnf配置文件创建根证书自签发泛域名证书将crt转pem格式生成 p12 格式的
当然上述的公钥制作方式需要交互式输入信息,如果不想频繁输入,那么可以使用如下命令:
踏歌行
2020/10/27
9.2K0
在Linux下如何根据域名自签发OpenSSL证书与常用证书转换
    




        修改openssl.cnf配置文件创建根证书自签发泛域名证书将crt转pem格式生成 p12 格式的
相关推荐
【错】urllib.error.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED]
更多 >
领券
💥开发者 MCP广场重磅上线!
精选全网热门MCP server,让你的AI更好用 🚀
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
本文部分代码块支持一键运行,欢迎体验
本文部分代码块支持一键运行,欢迎体验