What is Requests
The module author @kennethreitz.
From their website:
Requests is an Apache2 Licensed HTTP library, written in Python, for humans. Yes, that is their official tag line.
Python’s standard urllib2 module provides most of the HTTP capabilities you will ever need, but the Python implementation of the urllib2 API is quite broken.
Python's HTTP library was built for a different time when the Internet was young and still developing. We have moved to a much more RESTful, asynchronous model, which makes the most sense since the web is state-less.
urllib2 requires an enormous amount of work (even method overrides) to perform the simplest of tasks.
In order to make Python speak 'Internet' better for today's web applications and myriad of web frameworks, something had to be done. And it had to be done in the most Pythonic way possible. Introducing Requests.
Here is a sample... shell script.
>>> pip install requests >>> import requests >>> r = requests.get('https://encrypted.google.com/') >>> r.status_code 200 >>> r.headers['content-type'] 'text/html; charset=ISO-8859-1' >>> r.encoding 'ISO-8859-1' >>> r.text() <!DOCTYPE html><html itemtype="http://schema.org/WebPage/" lang="en">
Without Requests, you would have to use the Python standard library module urllib2 and your code would look more like this:
From the Python shell:
>>> import urllib2 >>> my_url = 'https://encrypted.google.com' >>> r = urllib2.Request(my_url) >>> a_mgr = urllib2.HTTPBasicAuthHandler(r) >>> url_opener = urllib2.build_opener(a_mgr) >>> open_stream = urllib2.install_opener(url_opener) >>> handler = urllib2.urlopen(r) >>> print(handler.getcode()) 200 print(handler.headers.getheader('content-type')) 'text/html'
Even if you optimized that a bit and shortened it to this group of statements.
In the Python shell:
>>> import urllib2 >>> from base64 import b64encode >>> request = urllib2.Request('https://encrypted.google.com/') >>> request.add_header('Authorization', 'Basic ' + b64encode('user' + ':' + 'pass')) >>> r = urllib2.urlopen(request) >>> print r.getcode() 200 >>> print(r.headers["content-type"]) 'text/html, charset=ISO-8859-1' >>> print(r.headers["X-RateLimit-Limit"]) 5
Requests still wins in simplicity and ease of use.
Requests takes all of the work out of Python HTTP/1.1 and makes integrating with web services straight-forward and seamless. You don't have to manually add query strings variables to your URLs, or to url-form-encode your POST data. All of that is handled for you by Requests. Keep-alive and HTTP connection pooling are fully automated. Requests is powered by urllib3, which is embedded within Requests.
Requests. HTTP for Humans