Python › Module 2 › Lesson 2
HTTP Requests (requests library)
Fetch URLs, inspect status codes and headers, and use requests for defensive checks
Opening
Defenders speak HTTP every day
Is the site up? Does it redirect to HTTPS? What Server header did it return? Python requests makes these checks scriptable for monitoring and triage—again, only on assets you are allowed to probe.
1. Install & Basic GET
Install requests (once)pip install requests pip3 install requests
pip install requests pip3 install requests
Fetch a page and print statusimport requests url = "https://example.com" r = requests.get(url, timeout=5) print(r.status_code) print(r.headers.get("server")) print(r.headers.get("strict-transport-security"))
import requests
url = "https://example.com"
r = requests.get(url, timeout=5)
print(r.status_code)
print(r.headers.get("server"))
print(r.headers.get("strict-transport-security"))2. Security-minded Options
Always set timeout=. Decide if redirects should be followed (allow_redirects=). Do not disable TLS verify for production checks unless you understand the risk (verify=False is dangerous). Use requests for your own sites, staging environments, and lab apps—not for blasting strangers.
Headers tell stories
Missing HSTS, odd servers, or unexpected redirects are quick defensive signals you can script.
Knowledge Check
requests.get(url) is used to:
Multiple choice
Knowledge Check
True or False: You should usually set a timeout on HTTP requests in scripts.
True or False
Knowledge Check
r.status_code tells you:
Multiple choice