python接口测试_python接口测试之http请求

8d76933cd976e1abf21286ee28592eb7.png

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:Python27python.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'状态的状态码:',r.status print u'是否请求Ok:',r.reason print u'header是多少:',r.getheaders() print u'Response消息结构:',r.msg print u'响应内容:',r.read()

getBaidu()

见输出的结果内容:

C:Python27python.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=.baidu.com

Set-Cookie: BIDUPSID=5BE300935709382ADC6AFA01D08E1959; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com

Set-Cookie: PSTM=1481724972; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.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:',r.getcode(),r.msg print u'headers:',r.headers

get_baidu()

见输出结果的内容:

C:Python27python.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=.baidu.com

Set-Cookie: BIDUPSID=55980CB92E6F9F88BF0FF2B73E8607D4; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com

Set-Cookie: PSTM=1481727148; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.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=.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:',r.getcode(),r.msg print u'headers:',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:Python27python.exe D:/git/Python/bookDay/api/http/httplipTest.py200 OK

{"status":true,"homeUrl":"/xian"}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值