C

Python › Module 2 › Lesson 2

BeginnerModule 2Lesson 2/5

HTTP Requests (requests library)

Fetch URLs, inspect status codes and headers, and use requests for defensive checks

15 min+53 XP3 quiz
Module progress2 of 5
https://🔒
HTTP GET · Status · Headers

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

1

requests.get(url) is used to:

Multiple choice

Knowledge Check

2

True or False: You should usually set a timeout on HTTP requests in scripts.

True or False

Knowledge Check

3

r.status_code tells you:

Multiple choice

← Previous

Answer all 3 knowledge checks to continue. (0/3 answered)