Paul Biggar Paul Biggar - 2 years ago 90
Python Question

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

In Python, what are the differences between the

, and
module? Why are there three? They seem to do the same thing...

Answer Source

I know its been said already, but I'd highly recommend the Requests python package:

If you've used languages other than python, you're probably thinking urllib and urllib2 are easy to use, not much code, and highly capable, that's how I used to think. But the Requests package is so unbelievably useful and short that everyone should be using it.

First, it supports a fully restful API, and is as easy as:

import requests

resp = requests.get('')
resp ='')
resp = requests.put('')
resp = requests.delete('')

Regardless of whether GET/POST you never have to encode parameters again, it simply takes a dictionary as an argument and is good to go.

userdata = {"firstname": "John", "lastname": "Doe", "password": "jdoe123"}
resp ='', params=userdata)

Plus it even has a built in json decoder (again, i know json.loads() isn't a lot more to write, but this sure is convenient):


Or if your response data is just text, use:


This is just the tip of the iceberg. This is the list of features from the requests site:

  • International Domains and URLs
  • Keep-Alive & Connection Pooling
  • Sessions with Cookie Persistence
  • Browser-style SSL Verification
  • Basic/Digest Authentication
  • Elegant Key/Value Cookies
  • Automatic Decompression
  • Unicode Response Bodies
  • Multipart File Uploads
  • Connection Timeouts
  • .netrc support
  • List item
  • Python 2.6—3.4
  • Thread-safe.
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download