What is the difference between urllib and urllib2 modules of Python?

來源:互聯網
上載者:User

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

urllib and urllib2 are both Python modules that do URL request related stuff but offer different functionalities. Their two most significant differences are listed below:

 

  • urllib2 can accept a Request 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 provides the urlencode method which is used for the generation of GET query strings,urllib2 doesn't have such a function. This is one of the reasons why urllib is often used along withurllib2.

For other differences between urllib and urllib2 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

  1. What is User Agent?
  2. What is the difference between GET and POST request methods?
  3. What is HTTP?

References

  1. urllib
  2. urllib2

原文:http://www.hacksparrow.com/python-difference-between-urllib-and-urllib2.html

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.