The main difference analysis between Python library urllib and Urllib2

Source: Internet
Author: User
Tags urlencode
As a Python rookie, has been ignorant of urllib and urllib2, thought 2 is 1 of the upgraded version. Today saw a foreigner wrote "Python:difference between Urllib and Urllib2" only understand the difference

You might is intrigued by the existence of the separate URLs modules in Python-urllib and URLLIB2. Even more intriguing:they is not alternatives for each other. So what's the difference between Urllib and urllib2, and do we need them both?

You may be curious about the two independent-urllib2 and-urllib2 in Python. What's even more interesting: they're not meant to be substituted for each other. So what's the difference between these two modules, and do we need both?

Urllib and Urllib2are both Python modules that does URL request related stuff but offer different functionalities. Their significant differences is listed below:

Urllib and URLLIB2 are all related modules that accept URL requests, but provide different functionality. The two most notable differences are as follows:

Urllib2 can accept a Request object to set the headers for a URL request,urllib accepts only a URL. That's means, you cannot masquerade your User Agent string etc.

Urllib2 can accept an instance of the request class to set the URL request headers,urllib only the URL can be accepted. This means that you can not disguise your user agent string and so on.

Urllib provides the UrlEncode method which is used for the generation of GET query strings, urllib2 doesn ' t has such a f Unction. This was one of the reasons why urllib are often used along with URLLIB2.

Urllib provides the UrlEncode method for generating a Get query string, and Urllib2 does not. This is why Urllib often uses it with URLLIB2.

For other differences between Urllib and Urllib2 refer to their documentations, the links is given in the References sect Ion.
Tip:if planning to do HTTP stuff only, check out httplib2, it's much better than Httplib or Urllib or URLLIB2.

Tip: If you're only doing HTTP-related, look at HTTPLIB2, which works better than several other modules.

Related information

Here's a similar question and answer: Urllib urllib2 What is the difference?

StackOverflow also have: should I use Urllib or urllib2?

Python Docs

    1. Urllib
    2. Urllib2
  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.