httpx的两个坑(httpx.ReadTimeout; SSL: CERTIFICATE_VERIFY_FAILED)
关于python异步网络请求库httpx的两个坑
其一:httpx.ReadTimeout
- 实测发现,网络不稳定的情况下,极其容易出现该错误。
- 相对于
requests库,httpx库是有默认的超时时间的。 - 参考方案: 初始化时将
timeout赋值为None
例1: client = httpx.AsyncClient(timeout=None)
例2: httpx.get(url=url, timeout=None)
# 默认配置
# location: /site-packages/httpx/_config.py:358
DEFAULT_TIMEOUT_CONFIG = Timeout(timeout=5.0)
# 附 Timeout 类
class Timeout:
"""
Timeout configuration.
**Usage**:
Timeout(None) # No timeouts.
Timeout(5.0) # 5s timeout on all operations.
Timeout(None, connect=5.0) # 5s timeout on connect, no other timeouts.
Timeout(5.0, connect=10.0) # 10s timeout on connect. 5s timeout elsewhere.
Timeout(5.0, pool=None) # No timeout on acquiring connection from pool.
# 5s timeout elsewhere.
"""
def __init__(
self,
timeout: typing.Union[TimeoutTypes, UnsetType] = UNSET,
*,
connect: typing.Union[None, float, UnsetType] = UNSET,
read: typing.Union[None, float, UnsetType] = UNSET,
write: typing.Union[None, float, UnsetType] = UNSET,
pool: typing.Union[None, float, UnsetType] = UNSET,
):
if isinstance(timeout, Timeout):
# Passed as a single explicit Timeout.
assert connect is UNSET
assert read is UNSET
assert write is UNSET
assert pool is UNSET
self.connect = timeout.connect # type: typing.Optional[float]
self.read = timeout.read # type: typing.Optional[float]
self.write = timeout.write # type: typing.Optional[float]
self.pool = timeout.pool # type: typing.Optional[float]
elif isinstance(timeout, tuple):
# Passed as a tuple.
self.connect = timeout[0]
self.read = timeout[1]
self.write = None if len(timeout) < 3 else timeout[2]
self.pool = None if len(timeout) < 4 else timeout[3]
elif not (
isinstance(connect, UnsetType)
or isinstance(read, UnsetType)
or isinstance(write, UnsetType)
or isinstance(pool, UnsetType)
):
self.connect = connect
self.read = read
self.write = write
self.pool = pool
else:
if isinstance(timeout, UnsetType):
raise ValueError(
"httpx.Timeout must either include a default, or set all "
"four parameters explicitly."
)
self.connect = timeout if isinstance(connect, UnsetType) else connect
self.read = timeout if isinstance(read, UnsetType) else read
self.write = timeout if isinstance(write, UnsetType) else write
self.pool = timeout if isinstance(pool, UnsetType) else pool
其二:httpx.ConnectError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: Hostname mismatch, certificate is not valid for '*'. (_ssl.c:1108)
- 在多域名或者通配符域名证书的情况下,可能会出现此类错误。
- 参考方案: 初始化时将
verify赋值为False
例1: client = httpx.AsyncClient(verify=False)
例2: httpx.get(url=url, verify=False)
# 默认配置
# `SSLConfig` 类中, `verify`默认值为 `True`
class SSLConfig:
"""
SSL Configuration.
"""
DEFAULT_CA_BUNDLE_PATH = Path(certifi.where())
def __init__(
self,
*,
cert: CertTypes = None,
verify: VerifyTypes = True,
trust_env: bool = True,
http2: bool = False,
):
pass
综上:
在引入
httpx包 初始化时,将以上两个参数先行传入。
class NewClass:
def __init__(self):
self.client = httpx.AsyncClient(verify=False, timeout=None)
_header = {
"User-Agent": "Mozilla/5.0 (M1 Mac OS X 12) Safari/666.66"
}
self.client.headers.update(_header)
httpx的两个坑(httpx.ReadTimeout; SSL: CERTIFICATE_VERIFY_FAILED)的更多相关文章
- 两个坑-Linux下Network-Manager有线未托管-DNS resolv.conf文件开机被清空
Linux里面有两套管理网络连接的方案: 1./etc/network/interfaces(/etc/init.d/networking) 2.Network-Manager 两套方案是冲突的,不能 ...
- iscroll遇到的两个坑
最近移动端闪付遇到的两个坑做下总结: 1.使用iscroll后,滑动并没有生效 解决方案: 首先要查看:结构是否正确: <div id="wrapper"> //w ...
- 记自己在spring中使用redis遇到的两个坑
本人在spring中使用redis作为缓存时,遇到两个坑,现在记录如下,算是作为自己的备忘吧,文笔不好,望大家见谅: 一.配置文件 <!-- 加载Properties文件 --> < ...
- MySql 5.7安装(随机密码,修改默认密码)两个坑
MySql 5.7安装(随机密码,修改默认密 下载了MySql 最新版本,安装的过程中,发现了很多新特性 1.data目录不见了 在进行my-default.ini配置的时候 (需要配置 # base ...
- [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed
1.在vs编写时出现这个问题(以下为网络查询结果) 问题的原因是“SSL: CERTIFICATE_VERIFY_FAILED”. Python 升级到 2.7.9 之后引入了一个新特性,当使用url ...
- python之https爬虫出现 SSL: CERTIFICATE_VERIFY_FAILED (同时打开fiddler就会出现)
1.参考 Py 坑之 CERTIFICATE_VERIFY_FAILED Python 升级到 2.7.9 之后引入了一个新特性,当你urllib.urlopen一个 https 的时候,会验证一次 ...
- 报错解决——SSL: CERTIFICATE_VERIFY_FAILED
SSL: CERTIFICATE_VERIFY_FAILED Python 升级到 2.7.9 之后引入了一个新特性,当使用urllib.urlopen打开一个 https 链接时,会验证一次 SSL ...
- pip或easy_install安装库报错:SSL: CERTIFICATE_VERIFY_FAILED
使用pip和easy_install安装那个lxml.pyspider这些库或者框架一直提示以下错误: Collecting pyspider Could not fetch URL https:// ...
- Python [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed 解决方法
一个搭建在SAE上的Django应用,使用新浪微博提供的Python SDK已经稳定运行一年有余,但最近开始持续出现微博认证失败的状况. 摘录微博python SDK的错误提示如下所示: ERROR: ...
- 豆瓣 URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:719)>
import urllib.request as urlrequest #import ssl#ssl._create_default_https_context = ssl._create_unve ...
随机推荐
- Laravel 代码开发最佳实践(持续更新)
我们这里要讨论的并不是 Laravel 版的 SOLID 原则(想要了解更多 SOLID 原则细节查看这篇文章)亦或是设计模式,而是 Laravel 实际开发中容易被忽略的最佳实践. 内容概览 单一职 ...
- Python 明明安装了Crypto模,但报错No module named “Crypto“
安装网上的解决方法卸载:pip uninstall cryptopip uninstall pycryptodomepip uninstall pycrypto重装:pip install Crypt ...
- 飞桨paddlespeech语音唤醒推理C实现
上篇(飞桨paddlespeech 语音唤醒初探)初探了paddlespeech下的语音唤醒方案,通过调试也搞清楚了里面的细节.因为是python 下的,不能直接部署,要想在嵌入式上部署需要有C下的推 ...
- 开心档之MySQL 复制表
MySQL 复制表 如果我们需要完全的复制MySQL的数据表,包括表的结构,索引,默认值等. 如果仅仅使用CREATE TABLE ... SELECT命令,是无法实现的. 本章节将为大家介绍如何完整 ...
- R-SVM-plot踩坑记录
并非所有的 svm 类型都支持plot.svm- 只有分类方法支持,而回归不支持. 所以代码应该svm_fit <- svm(factor(y)~x1+x2,data = df, kernel ...
- 基于create-react-app构建静态博客
前言: 用过hexo后,我被其强大的功能惊艳到了,于是便想自己也写一个静态博客生成器,并且可以发布到GitHub托管. 首先我们来看下效果图: 具体是怎么实现的呢? 我们通过create-react- ...
- count(列名)、count(1)和 count(*)有什么区别?
在MySQL中,这几个都是统计操作,很多人在使用的时候,都使用的是count(1),这有没有问题?使用正确?达到了统计效果? 我们从效果和效率两方面来分析下 执行效果 count(*) 包括了所有的列 ...
- JavaFx 关键字高亮文本实现
原文地址:JavaFx 关键字高亮文本实现 - Stars-One的杂货小窝 整蓝奏云批量下载器里的搜索功能想到的一个关键字高亮功能,借助textflow组件来实现,记录一下 本文基于TornadoF ...
- 2023-05-01:给你一个整数 n , 请你在无限的整数序列 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ...] 中找出并返回第 n 位上的数字。 1 <= n <=
2023-05-01:给你一个整数 n , 请你在无限的整数序列 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ...] 中找出并返回第 n 位上的数字. 1 <= n ...
- 2022-11-02:以下go语言代码输出什么?A:编译错误;B:apple;C:ant;D:panic。 package main import “fmt“ func main() {
2022-11-02:以下go语言代码输出什么?A:编译错误:B:apple:C:ant:D:panic. package main import "fmt" func main( ...