urllib urllib2 what is the difference ?

C

Chris Lyon

Could somebody please explain the difference between these two modules
and explain why they are both required, and if there will ever be a
unification of them?
 
T

Terry Carroll

Could somebody please explain the difference between these two modules
and explain why they are both required, and if there will ever be a
unification of them?

The main difference for me is that urllib2.urlopen can accept as an
argument, not only a URL name, but an instance of a Request class. The
Request class allows you to specify headers in the URL request. This is
helpful, for example, if you are retrieving a URL from a site that takes
different actions based on, say, the referral URL or the name of your
"browser", both of which are specified in request headers.

urllib.urlopen can only take a url string, and doesn't allow you (as far
as I can tell) to specify headers.

Another difference is the whole build_opener stuff that urllib2 provides,
but I've never had occasion to use or try to understand that, so I'll
leave that to someone else to explain.

On the flip side, urllib implements some things (like urlretrieve) that
urllib2 does not have; so urllib2 is not just a stepped-up urllib. That's
probably the reason they're not unified, at least for now (I have no
insight on future features).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top