python requests超时时间_python - 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 的
總結
以上是生活随笔為你收集整理的python requests超时时间_python - requests 库 使用过程中timeout值最大可设值?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 福临门家香味沂蒙土榨花生仁油使用的是土榨
- 下一篇: 红小豆最晚什么时候种?