Why doesn't requests.get() return? What is the default timeout that requests.get() uses?

后端 未结 6 1863
时光说笑
时光说笑 2020-11-28 05:09

In my script, requests.get never returns:

import requests

print (\"requesting..\")

# This call never returns!
r = requests.get(
    \"http://w         


        
6条回答
  •  半阙折子戏
    2020-11-28 05:38

    Patching the documented "send" function will fix this for all requests - even in many dependent libraries and sdk's. When patching libs, be sure to patch supported/documented functions, not TimeoutSauce - otherwise you may wind up silently losing the effect of your patch.

    import requests
    
    DEFAULT_TIMEOUT = 180
    
    old_send = requests.Session.send
    
    def new_send(*args, **kwargs):
         if kwargs.get("timeout", None) is None:
             kwargs["timeout"] = DEFAULT_TIMEOUT
         return old_send(*args, **kwargs)
    
    requests.Session.send = new_send
    

    The effects of not having any timeout are quite severe, and the use of a default timeout can almost never break anything - because TCP itself has default timeouts as well.

提交回复
热议问题