What is the difference between urllib and urllib2 modules of python?
You might be intrigued by the existence of two separate URL modules in Python-Urllib
AndUrllib2
. Even more intriguing: they are not alternatives for each other. So what is the differenceUrllib
AndUrllib2
, And do we need them both?
Urllib
AndUrllib2
Are both Python modules that do URL request related stuff but offer different functionalities. Their two most significant differences are listed below:
Urllib2
Can acceptRequest
Object To set the headers for a URL request,Urllib
Accepts only a URL. That means, you cannot masquerade your user agent string etc.
Urllib
ProvidesUrlencode
Method which is used for the generation of get query strings,Urllib2
Doesn't have such a function. This is one of the reasons whyUrllib
Is often used alongUrllib2
.
For other differencesUrllib
AndUrllib2
Refer to their documentations, the links are given in the references section.
Tip: If you are planning to do HTTP stuff only, check out httplib2, it is much better than httplib or urllib or urllib2.
Exercise
- What is user agent?
- What is the difference between get and POST request methods?
- What is HTTP?
References
- Urllib
- Urllib2
Original article: http://www.hacksparrow.com/python-difference-between-urllib-and-urllib2.html