When writing Python code that needs to make HTTP requests, you primarily have two options: the urllib and httplib libraries. Both allow you to open URLs and interact with web services, but they take slightly different approaches.
urllib: Simple and Batteries Included
The urllib package contains modules for working with URLs, including:
import urllib.request
with urllib.request.urlopen('http://example.com') as f:
print(f.read().decode('utf-8'))
The urllib module provides a simple, high level interface for fetching URLs and handling common errors. It's part of Python's standard library so you don't need to install anything else to use it.
httplib: Lower Level Access
The httplib module (renamed to http.client in Python 3) provides a more low-level interface to make HTTP requests. With httplib you deal directly with things like HTTP connections, request headers, and status codes.
import httplib
conn = httplib.HTTPSConnection("example.com")
conn.request("GET", "/")
r1 = conn.getresponse()
print(r1.status, r1.reason)
httplib allows full control over the HTTP conversation but requires more lines of code for simple operations.
When to Use Each?
Use urllib for basic HTTP needs:
Use httplib for more advanced cases:
The urllib module is easier to use for simple GET requests. But httplib allows full access to HTTP for advanced use cases. Pick the right tool for your next Python HTTP programming task!