首页 > requests 库 使用过程中timeout值最大可设值?

requests 库 使用过程中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 的超时时间设置,在 requests 的文档中已经描述的非常清楚了,可以是一个浮点数或者一个元组,在此就不再赘述。

timeout: (optional) How long to wait for the server to send data before giving up, as a float, or a (`connect timeout, read timeout`_) tuple.

【热门文章】
【热门文章】