Requests versus urllib2

Let's compare urllib2 and Requests; urllib2.urlopen(), which can be used to open a URL (which can be a string or a request object), but there are many other things that can be a burden while interacting with the web. At this point, a simple HTTP library which has the capabilities to make interaction with the web smooth is the need of the hour, and Requests is one of its kind.

The following is an example for fetching the data from a web service with urllib2 and Requests gives us a clear picture of how easy it is to work with Requests:

The following code gives an example of urllib2:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
 
import urllib2
 
gh_url = 'https://api.github.com'
 
req = urllib2.Request(gh_url)
 
password_manager = urllib2.HTTPPasswordMgrWithDefaultRealm()
password_manager.add_password(None, gh_url, 'user', 'pass')
 
auth_manager = urllib2.HTTPBasicAuthHandler(password_manager)
opener = urllib2.build_opener(auth_manager)
 
urllib2.install_opener(opener)
 
handler = urllib2.urlopen(req)
 
print handler.getcode()
print handler.headers.getheader('content-type')
 
# ------
# 200
# 'application/json'

The same example implemented with Requests:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
 
import requests
 
r = requests.get('https://api.github.com', auth=('user', 'pass'))
 
print r.status_code
print r.headers['content-type']
 
# ------
# 200
# 'application/json'

These examples can be found at https://gist.github.com/kennethreitz/973705.

At this initial stage, the example may look much complicated. Don't go deep into the details of the example. Just see the beauty of requests that allowed us to login to GitHub with very few lines of code. The code with requests seems much simpler and efficient than the urllib2 example. This would help us increase the productivity in all sorts of things.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.17.176.72