python requests超时时间_python - requests 库 使用过程中timeout值最大可设值?

问 题 请问 requests 库 使用过程中timeout错误的默认时间是多长呢? 查看源码 理论上 timeout值可以任意设? 默认 timeout 值

问 题

请问

requests 库 使用过程中timeout错误的默认时间是多长呢?

查看源码 理论上 timeout值可以任意设?

默认 timeout 值 是怎么来的呢?

如果我想设一个比较大的timeout 值 会生效吗?

解决方案

Requests 库是基于 urllib3 的,其连接的发起是调用了 urlopen 方法。其超时时间默认是 urllib3 中的 DEFAULT_TIMEOUT 决定。

在 urllib3中:

DEFAULT_TIMEOUT = _GLOBAL_DEFAULT_TIMEOUT

而 _GLOBAL_DEFAULT_TIMEOUT 的值是由 python 标准库 socket.py 决定的,在 socket.py 的源码中可以看到:

If no *timeout* is supplied, the global default timeout setting returned by :func:`getdefaulttimeout` is used.

getdefaulttimeout 函数在文档中已经有了非常明确地描述,你也可以点击这里查看:

Return the default timeout in seconds (float) for new socket objects. A value of None indicates that new socket objects have no timeout. When the socket module is first imported, the default is None.

也就是说当未指定超时时间时,默认的超时时间是 None,亦即连接永远不会超时。

对于 requests 的