python的强大之处在于提供了很多的标准库,这些标准库可以直接调用,本节部分,重点学习和总结在接口测试中Python的Http请求的库的学习。
首先来看httplib,官方的解释为:本模块定义了类实现客户端的 HTTP 和 HTTPS 协议。它通常不能直接使用 — — 模块 urllib 用于处理使用 HTTP 和 HTTPS 的 Url。使用httplib来做一个简单的对百度的请求,看这样的一个实现过程,见实现的代码:
#!/usr/bin/env python#coding:utf-8import httplibdef getBaidu():
http_client=httplib.HTTPConnection('baidu.com',80,timeout=20)
http_client.request('GET','')
r=http_client.getresponse() print r.status print r.read()
getBaidu()
这里我们详细的来看r提供了那些方法,见输出的结果:
#!/usr/bin/env python#coding:utf-8import httplib def getBaidu():
http_client=httplib.HTTPConnection('baidu.com',80,timeout=20)
http_client.request('GET','')
r=http_client.getresponse() print dir(r) getBaidu()见输出的结果内容:
C:\Python27\python.exe D:/git/Python/bookDay/api/http/httplipTest.py
['__doc__', '__init__', '__module__', '_check_close', '_method', '_read_chunked', '_read_status', '_safe_read', 'begin', 'chunk_left', 'chunked', 'close', 'debuglevel', 'fileno', 'fp', 'getheader', 'getheaders', 'isclosed', 'length', 'msg', 'read', 'reason', 'status', 'strict', 'version', 'will_close']
Process finished with exit code 0
使用dir(r)方法可以看到调用的方法,如我们想看到请求百度这样的一个GET请求,到底返回的status code是多少,是否OK,响应内容是什么,headers是什么,见实现的代码和输出的内容:
#!/usr/bin/env python#coding:utf-8import httplib def getBaidu():
http_client=httplib.HTTPConnection('www.baidu.com',80,timeout=20)
http_client.request('GET','/')
r=http_client.getresponse() print u'状态的状态码:\n',r.status print u'是否请求Ok:\n',r.reason print u'header是多少:\n',r.getheaders() print u'Response消息结构:\n',r.msg print u'响应内容:\n',r.read()
getBaidu()
见输出的结果内容:
C:\Python27\python.exe D:/git/Python/bookDay/api/http/httplipTest.py
状态的状态码:200是否请求Ok:
OK
header是多少:
[('content-length', '14613'), ('set-cookie', 'BAIDUID=5BE300935709382ADC6AFA01D08E1959:FG=1; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com, BIDUPSID=5BE300935709382ADC6AFA01D08E1959; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com, PSTM=1481724972; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com'), ('accept-ranges', 'bytes'), ('vary', 'Accept-Encoding'), ('server', 'BWS/1.1'), ('last-modified', 'Mon, 12 Dec 2016 06:09:00 GMT'), ('connection', 'Keep-Alive'), ('x-ua-compatible', 'IE=Edge,chrome=1'), ('pragma', 'no-cache'), ('cache-control', 'no-cache'), ('date', 'Wed, 14 Dec 2016 14:16:12 GMT'), ('p3p', 'CP=" OTI DSP COR IVA OUR IND COM "'), ('content-type', 'text/html')]
Response消息结构:
Date: Wed, 14 Dec 2016 14:16:12 GMT
Content-Type: text/html
Content-Length: 14613Last-Modified: Mon, 12 Dec 2016 06:09:00 GMT
Connection: Keep-Alive
Vary: Accept-Encoding
Set-Cookie: BAIDUID=5BE300935709382ADC6AFA01D08E1959:FG=1; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.http://baidu.com
Set-Cookie: BIDUPSID=5BE300935709382ADC6AFA01D08E1959; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.http://baidu.com
Set-Cookie: PSTM=1481724972; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.http://baidu.com
P3P: CP=" OTI DSP COR IVA OUR IND COM "Server: BWS/1.1X-UA-Compatible: IE=Edge,chrome=1Pragma: no-cache
Cache-control: no-cache
Accept-Ranges: bytes
下来来看urllib2的库,看官方的解释:urllib2 定义了很多函数和类,这些函数和类能够帮助我们在复杂的情况下获取URLS内容。复杂情况— 基本的和深入的验证, 重定向, cookies 等等.和如上一样,我们使用urllib2来实现对百度的请求,见GET请求的代码:
#!/usr/bin/env python#coding:utf-8import urllibimport urllib2 def get_baidu():
r=urllib2.urlopen('http://www.baidu.com') print u'Response code:\n',r.getcode(),r.msg print u'headers:\n',r.headers
get_baidu()
见输出结果的内容:
C:\Python27\python.exe D:/git/Python/bookDay/api/http/httplipTest.py
Response code:200 OK
headers:
Date: Wed, 14 Dec 2016 14:52:28 GMT
Content-Type: text/html; charset=utf-8Vary: Accept-Encoding
Set-Cookie: BAIDUID=55980CB92E6F9F88BF0FF2B73E8607D4:FG=1; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.http://baidu.com
Set-Cookie: BIDUPSID=55980CB92E6F9F88BF0FF2B73E8607D4; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.http://baidu.com
Set-Cookie: PSTM=1481727148; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.http://baidu.com
Set-Cookie: BDSVRTM=0; path=/Set-Cookie: BD_HOME=0; path=/Set-Cookie: H_PS_PSSID=1429_21422_21120_21553_21408_20927; path=/; domain=.http://baidu.com
P3P: CP=" OTI DSP COR IVA OUR IND COM "Cache-Control: private
Cxy_all: baidu+435ac15289339958b4bcff45cd341d25
Expires: Wed, 14 Dec 2016 14:51:52 GMT
X-Powered-By: HPHP
Server: BWS/1.1X-UA-Compatible: IE=Edge,chrome=1BDPAGETYPE: 1BDQID: 0xfc9210c000017b10BDUSERID: 0
Transfer-Encoding: chunked
Proxy-Connection: Close
下面使用urllib2实现一个POST的请求过程,见实现的代码:
#!/usr/bin/env python#coding:utf-8import urllibimport urllib2 def get_baidu():
r=urllib2.urlopen('http://www.baidu.com') print u'Response code:\n',r.getcode(),r.msg print u'headers:\n',r.headersdef post_cun():
params=urllib.urlencode({'cityId':'438'})
r=urllib2.urlopen('http://m.cyw.com/index.php?m=api&c=cookie&a=setcity',params) print r.getcode(),r.msg print r.read()post_cun()
见输出结果的json内容:
C:\Python27\python.exe D:/git/Python/bookDay/api/http/httplipTest.py200 OK
{"status":true,"homeUrl":"\/xian"}