Skip to content

HTTP Requests with the requests Library

Always use timeouts

requests_get.py
import requests
 
r = requests.get("https://httpbin.org/get", timeout=10)
r.raise_for_status()
print(r.status_code)
print(r.json())
requests_get.py
import requests
 
r = requests.get("https://httpbin.org/get", timeout=10)
r.raise_for_status()
print(r.status_code)
print(r.json())

Headers and User-Agent

requests_headers.py
import requests
 
headers = {"User-Agent": "PythonCentralHubBot/1.0"}
r = requests.get("https://httpbin.org/headers", headers=headers, timeout=10)
print(r.json())
requests_headers.py
import requests
 
headers = {"User-Agent": "PythonCentralHubBot/1.0"}
r = requests.get("https://httpbin.org/headers", headers=headers, timeout=10)
print(r.json())

Sessions (cookies + connection pooling)

requests_session.py
import requests
 
with requests.Session() as s:
    s.headers.update({"User-Agent": "MyBot/1.0"})
    r = s.get("https://httpbin.org/cookies/set?hello=world", timeout=10)
    r.raise_for_status()
 
    r2 = s.get("https://httpbin.org/cookies", timeout=10)
    print(r2.json())
requests_session.py
import requests
 
with requests.Session() as s:
    s.headers.update({"User-Agent": "MyBot/1.0"})
    r = s.get("https://httpbin.org/cookies/set?hello=world", timeout=10)
    r.raise_for_status()
 
    r2 = s.get("https://httpbin.org/cookies", timeout=10)
    print(r2.json())

๐Ÿงช Try It Yourself

Exercise 1 โ€“ List Files with os.listdir

Exercise 2 โ€“ Join Paths with os.path.join

Exercise 3 โ€“ Write and Read a File

If this helped you, consider buying me a coffee โ˜•

Buy me a coffee

Was this page helpful?

Let us know how we did