Urllib and Urllib2 and Httplib

Source: Internet
Author: User
Tags urlencode

Http://www.cnblogs.com/sysu-blackbear/p/3629420.html

Http://www.cnblogs.com/wly923/archive/2013/05/07/3057122.html

Tagged with: 2015-06-25 notes contents collated and updated to 51cto


Before using Python, access to the page Web site, are using curl, and then be supprocess the way, really stupid.

req = "Curl--max-time 5--connect-timeout 5-o/dev/null-s-W%{time_total},%{http_code}" + URL + "-H Host:" + CNAME

result = subprocess. Popen (req, Shell=true, stdout=subprocess. PIPE)

Stdout,stderr = Result.communicate ()


Later found that there are urlib urllib2 httplib and other Python-defined modules, gradually added to the use.

Data Source: http://www.hacksparrow.com/python-difference-between-urllib-and-urllib2.html

Python's urllib and urllib2 modules do all the work associated with requesting URLs, but they provide different functionality. The two most significant differences between them are as follows:

Urllib2 can accept a request object and use this to set the headers of a URL, but urllib only receives one URL. This means that you cannot disguise your user- agent string and so on. the urllib module can provide a method for UrlEncode, which is used to generate a get query string, and URLLIB2 does not have such a function. That's why Urllib and urllib2 often work together.

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.

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

The Urllib module provides a UrlEncode method for encapsulating content with key values and POST request content.  URLIB2, however, does not have this method. If you need a post field string, you need to use the Httplib method.

The Urllib.urlopen action object is only a URL address, while the urlib2.urlopen operand can be a request object, the parameter can be a URL address, a key-value pair post data, and a header-off field.

Httplib is the original request link, often linked applications, request content (similar to the Urllib2.urlopen function, postdata data can be non-key value to the content), link broken.


Example on line

#需要对网站CMS链接实时推送到百度站长平台 for automatic submission of proactive push. #filecontent为推送内容, a URL field is a row #domain  for the domain name   Both of these parameters are passed directly to the Baidu interface # Online curl ruby php method, the following self-written Python implementation # cannot use Urllib.urlencode to encapsulate the data, and then use the Urllib2.request assembly request # must use the Httplib object to encapsulate the request, Body content is   def postbaidu (filecontent, domain):         URL =  "/urls?site=%s&token=%s"  %  (Domain,othertoken)          send_headers = {' Content-type '  :  ' Text/plain '}         conn = httplib. Httpconnection (baiduurl)          #req  = urllib2. Request (url, data=data, headers=send_headers)          Conn.request (method= "POST",  url=url, body=filecontent, headers=send_headers)          response = conn.getresponse ()      &Nbsp;  baiduresult = response.read ()          Conn.close ()         return baiduresult



This article is from the "Spring and Autumn Preface" blog, please be sure to keep this source http://teemomo.blog.51cto.com/2376140/1665647

Urllib and Urllib2 and Httplib

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.