菜鸟笔记
提升您的技术认知

python httplib模块http请求详解-ag真人游戏

1. 介绍

httplib是提供了web客户端的功能和接口。这样httplib将会完成web浏览器的基本功能。

模块urllib,urllib2,httplib的区别(细节另起文章)
httplib实现了http和https的客户端协议,但是在python中,模块urllib和urllib2对httplib进行了更上层的封装。

2. 安装

[root@localhost ~]# pip install httplib2
downloading/unpacking httplib2
  downloading httplib2-0.17.0.tar.gz (220kb): 220kb downloaded
  running setup.py (path:/tmp/pip_build_root/httplib2/setup.py) egg_info for package httplib2
    
installing collected packages: httplib2
  running setup.py install for httplib2
    
successfully installed httplib2
cleaning up...

3. class httplib.httpconnection

3.1 httpconnection()

3.1.1 语法

 class httplib.httpconnection(host[,port[, strict[, timeout[, source_address]]]]) 

3.1.2 用法

该类用于创建一个http类型的请求链接

3.1.3 参数

  • host: 请求的服务器host,不能带http://开头
  • port: 服务器web服务端口
  • strict: 是否严格检查请求的状态行,就是http1.0/1.1
    协议版本的那一行,即请求的第一行,默认为false,为true时检查错误会抛异常。
  • timeout: 单次请求的超时时间,没有时默认使用httplib模块内的全局的超时时间

3.1.4 返回

httpconnection类会实例并返回一个httpconnection对象

3.1.5 详解

httpconnection的实例表示与http服务器的事务。实例化时需要传递主机和可选的端口号。如果没有端口号,试图以host:port格式从主机字符串提取,如果提取失败则使用默认的http端口(80)。
参数strict默认为false,表示在无法解析状态行时(status line)不能被http/1.0或1.1解析时不抛出badstatusline异常;可选参数timeout表示即阻塞在多少秒后超时,如果没有给出默认使用全局超时设置。可选参数source_address表示http的源地址(host, port)。

实例代代码:

import httplib
conn =httplib.httpconnection('www.coonote.com')
print conn
conn = httplib.httpconnection('www.coonote.com:80')
print conn conn =httplib.httpconnection('www.coonote.com','80')
print conn conn =httplib.httpconnection('www.coonote.com','80',true)
print conn conn =httplib.httpconnection('www.coonote.com','80',true,10)
print conn conn =httplib.httpconnection('www.coonote.com:80',true,10)
print conn

输出:







3.2 httpsconnection()

3.2.1 语法

httplib.httpsconnection('www.baidu.com',443,key_file,cert_file,true,10)

3.2.2 用法

该类用于创建一个https类型的请求链接

3.2.3 参数

  • key_file:一个包含pem格式的私钥文件
  • cert_file:一个包含pem格式的认证文件
  • other:其它同http参数

3.2.4 返回

同样返回一个httpsconnection对象

注意
要创建https链接,必须要保证底层的socket模块是支持ssl的编译模式,即编译时ssl选项的开关是开着的

3.2.5 详解

httpconnection的子类,使用ssl与安全服务器通信。默认端口为443。key_file是包含pem格式私钥的文件名称。 cert_file中是pem格式的证书链文件。

3.2.6 实例代码

import httplib
conn = httplib.httpsconnection('www.baidu.com',443,key_file,cert_file,true,10)

3.3 httpconnection.request()

3.3.1 语法

 httpconnection.request( method , url [ , body [ , headers ]] )

3.3.2 用法

调用request方法会向服务器发送一次请求

3.3.3 参数

  • method: 请求的方式,如’get’,‘post’,‘head’,‘put’,'delete’等
  • url: 请求的网页路径。如:‘/index.html’
  • body: 请求是否带数据,该参数是一个字典
  • headers: 请求是否带头信息,该参数是一个字典,不过键的名字是指定的http头关键字

3.3.4 返回

无返回,其实就是相对于向服务其发送数据,但是没有最后回车

3.3.5 代码

import httplib
conn =httplib.httpconnection('www.baidu.com:80',true,10)
print conn.request('get','/','',{
  'user-agent':'test'})

3.4 httpconnection.getresponse()

3.4.1 说明

获取一个http响应对象,相当于执行最后的2个回车

3.4.2 返回

httpresponse对象(下面会用到)

3.4.3 代码

[root@localhost httplib2]# cat http2.py
#!/usr/bin/python
import httplib
conn=httplib.httpconnection('www.baidu.com',80,false,10)
conn.request('get','/','',{
  'user-agent':'test'})
res = conn.getresponse()
print res
[root@localhost httplib2]# python http2.py

3.5 httpconnection.connect()

说明:对象创建之后连接到指定的服务器

3.6 httpconnection.close()

说明:关闭与服务器的连接
代码:

 #!/usr/bin/python
import httplib
conn=httplib.httpconnection('www.baidu.com',80,false,10)
conn.request('get','/','',{
  'user-agent':'test'})
res = conn.getresponse()
print res
conn.close()

3.7 httpconnection.set_debuglevel( level )

说明: 设置高度的级别。参数level 的默认值为0 ,表示不输出任何调试信息

 #!/usr/bin/python
import httplib
conn=httplib.httpconnection('www.baidu.com',80,false,10)
conn.request('get','/','',{
  'user-agent':'test'})
debug = conn.set_debuglevel(0)
print debug
conn.close()

4. class httplib.httpresponse

httpresponse表示服务器对客户端请求的响应。往往通过调用httpconnection.getresponse()来创建,实例连接成功之后返回的类,不能由用户实例化。
它有如下方法和属性:

4.1 httpresponse.read([amt])

说明: 获得http响应的内容部分,即网页源码
原型:body = res.read([amt])amt: 读取指定长度的字符,默认为空,即读取所有内容
返回:网页内容字符串
获取响应的消息体。如果请求的是一个普通的网页,那么该方法返回的是页面的html。可选参数amt表示从响应流中读取指定字节的数据。

[root@localhost httplib2]# cat http5.py
#!/usr/bin/python
import httplib
conn=httplib.httpconnection('www.baidu.com',80,false,10)
conn.request('get','')
res = conn.getresponse()
print res.read()
[root@localhost httplib2]# python http5.py
	
	
	
	·········

获取执指定的响应头。name表示头域(headerfield)名,可选参数default在头域名不存在的情况下作为默认值返回。

4.2 httpresponse.getheaders()

说明; 获得所有的响应头内容,是一个元组列表[(name,value),(name2,value2)]
附代码:

[root@localhost httplib2]# cat http6.py
#!/usr/bin/python
import httplib
conn=httplib.httpconnection('www.baidu.com',80,false,10)
conn.request('get','')
res = conn.getresponse()
print res.getheaders()
[root@localhost httplib2]# python http6.py
[('content-length', '14615'), ('traceid', '1585662997043926733812101108609502278751'), ('set-cookie', 'baiduid=c1da400a878388a271cc54bcde99f6f5:fg=1; expires=thu, 31-dec-37 23:55:55 gmt; max-age=2147483647; path=/; domain=.baidu.com, bidupsid=c1da400a878388a271cc54bcde99f6f5; expires=thu, 31-dec-37 23:55:55 gmt; max-age=2147483647; path=/; domain=.baidu.com, pstm=1585662997; expires=thu, 31-dec-37 23:55:55 gmt; max-age=2147483647; path=/; domain=.baidu.com, baiduid=c1da400a878388a2e4674a2d94b0bb5f:fg=1; max-age=31536000; expires=wed, 31-mar-21 13:56:37 gmt; domain=.baidu.com; path=/; version=1; comment=bd'), ('accept-ranges', 'bytes'), ('vary', 'accept-encoding'), ('server', 'bws/1.1'), ('connection', 'keep-alive'), ('x-ua-compatible', 'ie=edge,chrome=1'), ('pragma', 'no-cache'), ('cache-control', 'no-cache'), ('date', 'tue, 31 mar 2020 13:56:37 gmt'), ('p3p', 'cp=" oti dsp cor iva our ind com ", cp=" oti dsp cor iva our ind com "'), ('content-type', 'text/html')]

4.3 httpresponse.msg()

说明:获取所有的响应头信息。包含响应头的mimetools.message实例

[root@localhost httplib2]# cat http7.py
#!/usr/bin/python
import httplib
conn=httplib.httpconnection('www.baidu.com',80,false,10)
conn.request('get','')
res = conn.getresponse()
print res.msg
[root@localhost httplib2]# python http7.py
accept-ranges: bytes
cache-control: no-cache
connection: keep-alive
content-length: 14615
content-type: text/html
date: tue, 31 mar 2020 13:59:33 gmt
p3p: cp=" oti dsp cor iva our ind com "
p3p: cp=" oti dsp cor iva our ind com "
pragma: no-cache
server: bws/1.1
set-cookie: baiduid=03c55cb4b2f53eeb89bc70720aba5db8:fg=1; expires=thu, 31-dec-37 23:55:55 gmt; max-age=2147483647; path=/; domain=.baidu.com
set-cookie: bidupsid=03c55cb4b2f53eeb89bc70720aba5db8; expires=thu, 31-dec-37 23:55:55 gmt; max-age=2147483647; path=/; domain=.baidu.com
set-cookie: pstm=1585663173; expires=thu, 31-dec-37 23:55:55 gmt; max-age=2147483647; path=/; domain=.baidu.com
set-cookie: baiduid=03c55cb4b2f53eeb1d3f571ba752a14e:fg=1; max-age=31536000; expires=wed, 31-mar-21 13:59:33 gmt; domain=.baidu.com; path=/; version=1; comment=bd
traceid: 1585663173045617562611241334006733438813
vary: accept-encoding
x-ua-compatible: ie=edge,chrome=1

4.4 httpresponse.msg

说明:获取服务器所使用的http协议版本。11表示http/1.1;10表示http/1.0

[root@localhost httplib2]# cat http8.py
#!/usr/bin/python
import httplib
conn=httplib.httpconnection('www.baidu.com',80,false,10)
conn.request('get','')
res = conn.getresponse()
print res.version
[root@localhost httplib2]# python http8.py
11

4.5 httpresponse.status

说明: 获取响应的状态码。如:200表示请求成功

[root@localhost httplib2]# cat http9.py
#!/usr/bin/python
import httplib
conn=httplib.httpconnection('www.baidu.com',80,false,10)
conn.request('get','')
res = conn.getresponse()
print res.status
[root@localhost httplib2]# python http9.py
200

4.6 httpresponse.reason

说明:返回服务器处理请求的结果说明。一般为”ok”

[root@localhost httplib2]# cat http10.py
#!/usr/bin/python
import httplib
conn=httplib.httpconnection('www.baidu.com',80,false,10)
conn.request('get','')
res = conn.getresponse()
print res.reason
[root@localhost httplib2]# python http10.py
ok

5. class httplib.httpmessage

httpmessage实例用于保存http响应头。它使用mimetools.message类实现,并提供了处理http头的工具函数。它不直接实例化的用户。不能由用户实例化。

6. 异常处理

exception httplib.httpexception

exception的子类,此模块中的其他异常的基类。下面的类默认是该类的直接子类。

httplib.notconnected
httplib.invalidurl
httplib.unknownprotocol
httplib.unknowntransferencoding
httplib.unimplementedfilemode
httplib.incompleteread
httplib.improperconnectionstate
httplib.cannotsendrequest
improperconnectionstate的一个子类。
httplib.cannotsendheader
improperconnectionstate的一个子类。
httplib.responsenotready
improperconnectionstate的一个子类。
httplib.badstatusline

服务器返回的http状态码不认识时产生。

7. 实战

[root@localhost httplib2]# cat http11.py
#!/usr/bin/python
#coding:utf-8
import httplib, urllib
conn = none
try:
    params = urllib.urlencode({
  'name': 'qiye', 'age': 22})
    headers = {
  "content-type": "application/x-www-form-urlencoded"
    , "accept": "text/plain"}
    conn = httplib.httpconnection("www.zhihu.com", 80, timeout=3)
    conn.request("post", "/login", params, headers)
    response = conn.getresponse()
    print response.getheaders() # 获取头信息
    print response.status
    print response.read()
except exception, e:
    print e
finally:
    if conn:
        conn.close()
[root@localhost httplib2]# python http11.py
[('x-edge-timing', '0.000'), ('content-length', '278'), ('via', 'vcache3.cn2204[,0]'), ('x-cdn-provider', 'alibaba'), ('eagleid', '3ad79e1715856640307233330e'), ('server', 'tengine'), ('connection', 'keep-alive'), ('location', 'https://www.zhihu.com/login'), ('date', 'tue, 31 mar 2020 14:13:50 gmt'), ('content-type', 'text/html'), ('timing-allow-origin', '*')]
301
301 moved permanently

the requested resource has been assigned a new permanent uri.


powered by tengine
网站地图