In Python, what are the differences between the urllib, urllib2
, urllib3 and requests modules? Why are there three? They seem to do the same thing...
I think all answers are pretty good. But fewer details about urllib3.urllib3 is a very powerful HTTP client for python. For installing both of the following commands will work,
urllib3
using pip,
pip install urllib3
or you can get the latest code from Github and install them using,
$ git clone git://github.com/urllib3/urllib3.git
$ cd urllib3
$ python setup.py install
Then you are ready to go,
Just import urllib3 using,
import urllib3
In here, Instead of creating a connection directly, You’ll need a PoolManager instance to make requests. This handles connection pooling and thread-safety for you. There is also a ProxyManager object for routing requests through an HTTP/HTTPS proxy Here you can refer to the documentation. example usage :
>>> from urllib3 import PoolManager
>>> manager = PoolManager(10)
>>> r = manager.request('GET', 'http://google.com/')
>>> r.headers['server']
'gws'
>>> r = manager.request('GET', 'http://yahoo.com/')
>>> r.headers['server']
'YTS/1.20.0'
>>> r = manager.request('POST', 'http://google.com/mail')
>>> r = manager.request('HEAD', 'http://google.com/calendar')
>>> len(manager.pools)
2
>>> conn = manager.connection_from_host('google.com')
>>> conn.num_requests
3
As mentioned in urrlib3
documentations,urllib3
brings many critical features that are missing from the Python standard libraries.
Follow the user guide for more details.
requests
requests uses urllib3
under the hood and make it even simpler to make requests
and retrieve data.
For one thing, keep-alive is 100% automatic, compared to urllib3
where it's not. It also has event hooks which call a callback function when an event is triggered, like receiving a response
In requests
, each request type has its own function. So instead of creating a connection or a pool, you directly GET a URL.
For install requests
using pip just run
pip install requests
or you can just install from source code,
$ git clone git://github.com/psf/requests.git
$ cd requests
$ python setup.py install
Then, import requests
Here you can refer the official documentation, For some advanced usage like session object, SSL verification, and Event Hooks please refer to this url.