The Python requests library is one of the most popular ways to make HTTP requests in Python. Under the hood, requests relies on urllib3 to handle many aspects of making HTTP requests and handling responses. So what exactly is the relationship between requests and urllib3?
Requests is a Wrapper Around urllib3
The requests library provides an elegant, simple API for making HTTP requests in Python. When you call methods like requests.get() or requests.post(), requests handles:
Encoding parametersHandling headersParsing responsesManaging cookiesBut under the hood, requests delegates the actual network request and response handling to urllib3. So urllib3 handles:
Opening socketsManaging connectionsSending/receiving dataSSL/TLS encryptionSo in essence, requests provides a nice high-level interface, while urllib3 handles the low-level details of making network requests. requests wraps urllib3 to make it simpler to work with.
Why Use Requests Instead of urllib3 Directly?
Since requests builds on top of urllib3, why not just use urllib3 directly? There are a few reasons:
requests provides a much simpler and more intuitive API than urllib3.requests handles encoding parameters, handling cookies, and parsing responses for you automatically.requests integrates well with other Python libraries and tools.So in most cases, requests is the best choice for making HTTP requests. But urllib3 is still useful for advanced, low-level use cases.
Key Takeaways
requests is one of the most popular Python libraries for making HTTP requestsUnder the hood, requests uses urllib3 to handle opening connections and transferring datarequests wraps urllib3 with a simpler, higher-level interfaceFor most tasks, requests is preferable to using urllib3 directlySo in summary, requests builds on urllib3 to provide a great high-level interface for making HTTP requests in Python.