Skip to content

Instantly share code, notes, and snippets.

@de1o
Created April 22, 2018 10:48
Show Gist options
  • Save de1o/ed01a4621f5429b36aedd5f391e97eb0 to your computer and use it in GitHub Desktop.
Save de1o/ed01a4621f5429b36aedd5f391e97eb0 to your computer and use it in GitHub Desktop.
add default timeout time for requests
import requests
def _hook_requests():
try:
from requests.sessions import Session
except ImportError:
return
real_send = Session.send
def send(self, request, *args, **kwargs):
if not kwargs.get('timeout'):
kwargs['timeout'] = 3
return real_send(self, request, *args, **kwargs)
Session.send = send
_hook_requests()
r = requests.get("http://localhost:5002/")
print(r.text)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment