Analysis of main differences between Urllib and urllib2 in Python library _python

Source: Internet
Author: User
Tags urlencode in python

As a Python rookie, has been ignorant of urllib and urllib2, thought 2 is 1 of the upgraded version. Today, I saw the foreigner wrote an article "Python:difference between Urllib and Urllib2" to understand the difference between

You might is intrigued by the existence of two separate URL modules in Python-urllib and URLLIB2. Even more intriguing:they are is not alternatives to each other. So what are the difference between urllib and urllib2, and do we need them?

You may be curious about the two independent-urllib2 and-urllib2 in Python. What's even more interesting is that they don't have to be substituted for each other. So what's the difference between these two modules, and do we both need them?

Urllib and Urllib2are both Python modules that do URL request related stuff the offer but different. Their two most significant differences are:

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 a request class to set the URL request Headers,urllib can only accept URLs. This means that you can not disguise your user agent string, and so on.

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

Urllib provides a UrlEncode method for the generation of a Get query string, while URLLIB2 does not. This is why urllib often used with URLLIB2.

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

Tip: If you only do HTTP-related, look at the httplib2, better than a few other modules.

Related information

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

StackOverflow also: 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.