关于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)的更多相关文章

  1. 两个坑-Linux下Network-Manager有线未托管-DNS resolv.conf文件开机被清空

    Linux里面有两套管理网络连接的方案: 1./etc/network/interfaces(/etc/init.d/networking) 2.Network-Manager 两套方案是冲突的,不能 ...

  2. iscroll遇到的两个坑

    最近移动端闪付遇到的两个坑做下总结: 1.使用iscroll后,滑动并没有生效 解决方案: 首先要查看:结构是否正确: <div id="wrapper">   //w ...

  3. 记自己在spring中使用redis遇到的两个坑

    本人在spring中使用redis作为缓存时,遇到两个坑,现在记录如下,算是作为自己的备忘吧,文笔不好,望大家见谅: 一.配置文件 <!-- 加载Properties文件 --> < ...

  4. MySql 5.7安装(随机密码,修改默认密码)两个坑

    MySql 5.7安装(随机密码,修改默认密 下载了MySql 最新版本,安装的过程中,发现了很多新特性 1.data目录不见了 在进行my-default.ini配置的时候 (需要配置 # base ...

  5. [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed

    1.在vs编写时出现这个问题(以下为网络查询结果) 问题的原因是“SSL: CERTIFICATE_VERIFY_FAILED”. Python 升级到 2.7.9 之后引入了一个新特性,当使用url ...

  6. python之https爬虫出现 SSL: CERTIFICATE_VERIFY_FAILED (同时打开fiddler就会出现)

    1.参考 Py 坑之 CERTIFICATE_VERIFY_FAILED Python 升级到 2.7.9 之后引入了一个新特性,当你urllib.urlopen一个 https 的时候,会验证一次 ...

  7. 报错解决——SSL: CERTIFICATE_VERIFY_FAILED

    SSL: CERTIFICATE_VERIFY_FAILED Python 升级到 2.7.9 之后引入了一个新特性,当使用urllib.urlopen打开一个 https 链接时,会验证一次 SSL ...

  8. pip或easy_install安装库报错:SSL: CERTIFICATE_VERIFY_FAILED

    使用pip和easy_install安装那个lxml.pyspider这些库或者框架一直提示以下错误: Collecting pyspider Could not fetch URL https:// ...

  9. Python [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed 解决方法

    一个搭建在SAE上的Django应用,使用新浪微博提供的Python SDK已经稳定运行一年有余,但最近开始持续出现微博认证失败的状况. 摘录微博python SDK的错误提示如下所示: ERROR: ...

  10. 豆瓣 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 ...

随机推荐

  1. 基于SqlSugar的开发框架循序渐进介绍(26)-- 实现本地上传、FTP上传、阿里云OSS上传三者合一处理

    在前面介绍的随笔<基于SqlSugar的开发框架循序渐进介绍(7)-- 在文件上传模块中采用选项模式[Options]处理常规上传和FTP文件上传>中介绍过在文件上传处理的过程中,整合了本 ...

  2. 23.04.06_blog能被搜索到

    博客优化内容 对于刚建立的博客来说,谷歌往往不能或者不会收录你的博客,为了使自己的博客可以被谷歌所检索到.我们需要主动向谷歌提供网址信息. 提交到百度搜索 访问百度搜索资源平台官网,注册或者登陆百度账 ...

  3. 记一次 .NET 某外贸ERP 内存暴涨分析

    一:背景 1. 讲故事 上周有位朋友找到我,说他的 API 被多次调用后出现了内存暴涨,让我帮忙看下是怎么回事?看样子是有些担心,但也不是特别担心,那既然找到我,就给他分析一下吧. 二:WinDbg ...

  4. GDB使用简单总结

    简单总结常用gdb调试命令 不长篇讨论gdb是什么,或者怎么使用了,因为网上很多都讲的比较详细,以下只是做个备录,经常使用的命令,偶尔不用容易忘记! 1.set args xxxx  (xxx为参数) ...

  5. Locust 任务类介绍

    前言: 任务:简单的理解就是,你想要你脚本的虚拟用户去做哪些事,比如请求某一个接口,或者执行某一个事件 用户:可以理解为,执这个任务的实例主体,或者在locust 中,也可以认为是一群蝗虫 一.Tas ...

  6. Prism Sample 10 10-CustomRegistrations

    作用同上节,这里是用修改注册的方式自定义View和ViewModel的关联. protected override void ConfigureViewModelLocator() { base.Co ...

  7. Keepalived 高可用

    Keepalived 高可用 什么是高可用 一般是指2台机器启动着完全相同的业务系统,当有一台机器down机了,另外一台服务器就能快速的接管,对于访问的用户是无感知的. 高可用通常使用的软件 keep ...

  8. 2023-04-12:使用 Go 重写 FFmpeg 的 extract_mvs.c 工具程序,提取视频中的运动矢量信息。

    2023-04-12:使用 Go 重写 FFmpeg 的 extract_mvs.c 工具程序,提取视频中的运动矢量信息. 答案2023-04-12: 主要的过程包括: 打开输入视频文件并查找视频流信 ...

  9. 2023-02-24:请用go语言调用ffmpeg,解码mp4文件并保存为YUV420SP格式文件,采用YUV420P转YUV420SP的方式。

    2023-02-24:请用go语言调用ffmpeg,解码mp4文件并保存为YUV420SP格式文件,采用YUV420P转YUV420SP的方式. 答案2023-02-24: 使用 github.com ...

  10. 2022-10-25:在一个 2 * 3 的板上(board)有 5 块砖瓦,用数字 1~5 来表示, 以及一块空缺用 0 来表示。一次 移动 定义为选择 0 与一个相邻的数字(上下左右)进行交换.

    2022-10-25:在一个 2 * 3 的板上(board)有 5 块砖瓦,用数字 1~5 来表示, 以及一块空缺用 0 来表示.一次 移动 定义为选择 0 与一个相邻的数字(上下左右)进行交换. ...