码迷,mamicode.com
首页 > Web开发 > 详细

urllib and urllib2 and httplib

时间:2015-06-25 17:40:00      阅读:156      评论:0      收藏:0      [点我收藏+]

标签:teemo's python urllib urllib2 httplib usage

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

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

标记: 2015-06-25  笔记内容整理和更新到51cto


 之前使用python中,访问页面网站时,都是使用curl, 再被supprocess的方式,真的很笨. 

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()


后来才发现原来还有urlib urllib2 httplib等python定义模块, 逐渐加入到使用中.

数据来源: http://www.hacksparrow.com/python-difference-between-urllib-and-urllib2.html

Python的urllib和urllib2模块都做与请求URL相关的操作,但他们提供不同的功能。他们两个最显着的差异如下:

       urllib2可以接受一个Request对象,并以此可以来设置一个URL的headers,但是urllib只接收一个URL。这意味着,你不能伪装你的用户代理字符串等。urllib模块可以提供进行urlencode的方法,该方法用于GET查询字符串的生成,urllib2的不具有这样的功能。这就是urllib与urllib2经常在一起使用的原因

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 with urllib2.

      urllib 模块提供urlencode方法,可以键值对内容进行封装处理, post请求内容. 而urlib2则没有此方法.  如果需要post字段串, 则需要使用httplib方法. 

     urllib.urlopen 操作对象仅为url地址,而urlib2.urlopen操作对象可以是一个Request对象,参数可以为URL地址,键值对post数据, header关字段.

      httplib 则为原始请求链接, 经常链接申请, 请求内容(类似urllib2.urlopen功能, postdata数据可以为非键值对内容),链接断开.


线上例子

#需要对网站CMS链接实时推送到百度站长平台,实现自动提交主动推送功能.
#filecontent为推送内容,一个URL字段为一行
#domain 为域名 这两个参数都会直接传入到baidu接口
#网上curl ruby php方法,以下自己写的python实现
#不能使用urllib.urlencode封装数据,再使用urllib2.Request组装请求
#必须使用httplib对象去封装请求,body内容为  
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()
        baiduresult = response.read()
        conn.close()
        return baiduresult



本文出自 “春秋代序” 博客,请务必保留此出处http://teemomo.blog.51cto.com/2376140/1665647

urllib and urllib2 and httplib

标签:teemo's python urllib urllib2 httplib usage

原文地址:http://teemomo.blog.51cto.com/2376140/1665647

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!