python--httplib模块使用
来源:互联网 发布:农村淘宝家乡版下载 编辑:程序博客网 时间:2024/05/22 08:14
httplib是一个相对底层的http请求模块,其上有专门的包装模块,如urllib内建模块,goto等第三方模块,但是封装的越高就越不灵活,比如urllib模块里请求错误时就不会返回结果页的内容,只有头信息,对于某些需要检测错误请求返回值的场景就不适用,所以就得用这个模块了。
1、class httplib.HTTPConnection
该类用于创建一个http类型的请求链接
原型:
HTTPConnection(host[, port[, strict[, timeout]]])
host: 请求的服务器host,不能带http://开头
port: 服务器web服务端口
strict: 是否严格检查请求的状态行,就是http1.0/1.1 协议版本的那一行,即请求的第一行,默认为False,为True时检查错误会抛异常
timeout: 单次请求的超时时间,没有时默认使用httplib模块内的全局的超时时间
实例: conn1 = HTTPConnection('www.baidu.com:80') conn2 = HTTPconnection('www.baidu.com',80) conn3 = HTTPConnection('www.baidu.com',80,True,10) 错误实例: conn3 = HTTPConnection('www.baidu.com:80',True,10)
返回:
HTTPConnection类会实例并返回一个HTTPConnection对象
2、class httplib.HTTPSConnection
说明:
该类用于创建一个https类型的请求链接
原型:
HTTPSConnection(host[, port[, key_file[, cert_file[, strict[, timeout]]]]])
key_file:一个包含PEM格式的私钥文件
cert_file:一个包含PEM格式的认证文件
other:其它同http参数
实例:
conn3 = HTTPSConnection('accounts.google.com',443,key_file,cert_file,True,10)
返回:
同样返回一个HTTPSConnection对象
注意:
要创建https链接,必须要保证底层的socket模块是支持ssl的编译模式,即编译时ssl选项的开关是开着的
3、HTTPConnection对象request方法:
说明:
发送一个请求
原型:
conn.request(method, url[, body[, headers]])
method: 请求的方式,如'GET','POST','HEAD','PUT','DELETE'等
url: 请求的网页路径。如:'/index.html'
body: 请求是否带数据,该参数是一个字典
headers: 请求是否带头信息,该参数是一个字典,不过键的名字是指定的http头关键字
实例:
conn.request('GET', '/', '', {'user-agent':'test'})返回:
无返回,其实就是相对于向服务其发送数据,但是没有最后回车
4、HTTPConnection对象getresponse方法
说明:
获取一个http响应对象,相当于执行最后的2个回车
原型/实例:
res = conn.getresponse()
返回:
HTTPResponse对象
5、HTTPConnection对象close()方法
说明:
关闭指定的httpconnect链接
实例:
conn.close()
6、HTTPResponse对象read方法
说明:
获得http响应的内容部分,即网页源码
原型:
body = res.read([amt])
amt: 读取指定长度的字符,默认为空,即读取所有内容
实例:
body = res.read() pbody = res.read(10)
返回:
网页内容字符串
7、HTTPResponse对象的其它方法或属性
方法:
getheaders()
获得所有的响应头内容,是一个元组列表[(name,value),(name2,value2)]
getheader(name[,default])
获得指定的头内容
fileno()
socket的fileno
属性:
msg
所有的头信息,和getheaders方法一样,只不过这个是原始未处理的字符串
status
当次请求的状态
version
当次请求的http协议版本,10是http1.0, 11是http/1.1
reason
当次请求的结果的表述内容,200是ok,404是Not Found
总体实例:
#!/usr/bin/env python # -*- coding: utf-8 -*- import httplib import urllib def sendhttp(): data = urllib.urlencode({'@number': 12524, '@type': 'issue', '@action': 'show'}) headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"} conn = httplib.HTTPConnection('bugs.python.org') conn.request('POST', '/', data, headers) httpres = conn.getresponse() print httpres.status print httpres.reason print httpres.read() if __name__ == '__main__': sendhttp()
还有一个获取头部信息的简单例子
import httpliburl = "www.baidu.com"conn = httplib.HTTPConnection(url)conn.request("GET", "/")r=conn.getresponse()h = r.getheaders()for hh in h: print hh
当然了还可以使用urllib2模块,来获取头部信息
from urllib2 import urlopen, HTTPErrorwebURL = 'http://www.sina.com'try: res = urlopen(webURL) print res.info() print res.geturl()except HTTPError as e: print e.codeexcept: print "Error!!"
当然最简单的方法就是用curl命令啦
curl --head http://www.baidu.com
输出:
HTTP/1.1 200 OKDate: Wed, 10 Apr 2013 10:12:35 GMTServer: BWS/1.0Content-Length: 10320Content-Type: text/html;charset=utf-8Cache-Control: privateExpires: Wed, 10 Apr 2013 10:12:35 GMTSet-Cookie: H_PS_PSSID=1467_1945_1788_2141_2209; path=/; domain=.baidu.comSet-Cookie: BAIDUID=3FB8134FCB07E7C1E557B841CAA1596E:FG=1; expires=Wed, 10-Apr-43 10:12:35 GMT; path=/; domain=.baidu.comP3P: CP=" OTI DSP COR IVA OUR IND COM "Connection: Keep-Alive
当然还有其它的一些信息,比如异常类型,比如http的code对应表及查询字典等等,可以直接参考官网httplib文档:http://docs.python.org/library/httplib.html
- python--httplib模块使用
- python--httplib模块使用
- python--httplib模块使用
- python--httplib模块使用
- python--httplib模块使用
- python--httplib模块使用
- python--httplib模块使用
- python--httplib模块使用
- python--httplib模块使用
- python--httplib模块使用
- python--httplib模块的使用
- Python httplib模块
- python httplib 模块
- python之httplib模块
- Httplib模块使用
- python调用httplib,urllib2模块
- python httplib urllib的使用
- Python模块之---urllib,urllib2,httplib
- 到底应该把网站放在哪里?
- hadoop ant 异常
- C字符串和C++中string的区别
- C语言也能干大事第十三节(如鹏基础)
- 闲聊linux中的input设备(1)Linux中的设备大家族
- python--httplib模块使用
- Nginx配置文件详细说明
- stm32 独立看门狗[操作寄存器+库函数]
- ASP.NET MVC的JSON序列化陷阱
- MyEclipse6.5安装SVN插件的三种方法(转载)
- 闲聊linux中的input设备(1)Linux中的设备大家族
- Solr分布式搜索技术实现分析
- C++读取以逗号为分隔符的数值
- Myeclipse8.5性能优化