python requests timeout default code example

Example 1: python requests.get timeout

# Here is how to set a time out for requests.get in python
# its simple!
import requests

link = 'https://google.com' 
request_from_link = requests.get(link, timeout=10) 
# this causes the code to call a timeout if the connection or delays in 
# between the reads take more than 10 seconds
print(request_from_link)

Example 2: requests sessions

s = requests.Session()
s.auth = ('user', 'pass')
s.headers.update({'x-test': 'true'})

# both 'x-test' and 'x-test2' are sent
s.get('https://httpbin.org/headers', headers={'x-test2': 'true'})

Example 3: requests sessions

s = requests.Session()

s.get('https://httpbin.org/cookies/set/sessioncookie/123456789')
r = s.get('https://httpbin.org/cookies')

print(r.text)
# '{"cookies": {"sessioncookie": "123456789"}}'