The difference between Python:urllib and URLLIB2 (RPM)

Source: Internet
Author: User
Tags urlencode

Original link :http://www.cnblogs.com/yuxc/

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 urllib2 and, and do we need them both?


urllib  and   urllib2are Both Python modules this do 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:

  • urllib2Can 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.

  • urllibProvides urlencode the method which is used for the generation of GET query strings, urllib2 doesn ' t has such a function. This is one of the reasons, why are urllib 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 urllib2 and refer to their documentations, the links is given in the References section .

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

Differences between Python:urllib and URLLIB2 (ext.)

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.