Master urllib2: Using info(), geturl() and Handlers for HTTP Requests
This guide explains urllib2's info() and geturl() methods, demonstrates how to work with Openers and Handlers, and shows how to implement HTTP Basic Authentication with practical Python examples.
Understanding urllib2's info() and geturl()
The response object returned by urlopen (or an HTTPError instance) provides two useful methods: info() and geturl().
1. geturl()
Returns the final URL after any redirects, which is essential when the requested URL differs from the actual one fetched.
Example: a script urllib2_test10.py compares the original URL with the redirected URL, showing the true destination.
2. info()
Returns an httplib.HTTPMessage instance containing the response headers, such as Content‑Length and Content‑Type .
A script urllib2_test11.py prints these headers, illustrating how to retrieve page metadata.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
