What are the differences between the urllib, urllib2, urllib3 and requests module?

后端 未结 11 2444
隐瞒了意图╮
隐瞒了意图╮ 2020-11-22 04:19

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...

11条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-22 04:51

    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.

    • Thread safety.
    • Connection pooling.
    • Client-side SSL/TLS verification.
    • File uploads with multipart encoding.
    • Helpers for retrying requests and dealing with HTTP redirects.
    • Support for gzip and deflate encoding.
    • Proxy support for HTTP and SOCKS.
    • 100% test coverage.

    Follow the user guide for more details.

    • Response content (The HTTPResponse object provides status, data, and header attributes)
    • Using io Wrappers with Response content
    • Creating a query parameter
    • Advanced usage of urllib3

    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.

提交回复
热议问题