Python在HTTP接口测试中的应用
上一篇 /
下一篇 2012-07-10 15:02:09
/ 个人分类:Python
HTTP接口例子:http:\\ip:port\interface.php?uname=aaa51Testing软件测试网
ve3SN%@V
接口功能: 根据uname参数值来返回对应的用户名的基本信息
51Testing软件测试网 v?/c&j:h2f[xYvhG1. 用Python封装被测试接口,对于HTTP接口我们通常会采用 GET和POST 2种调用方式去访问,所以必须把这2种方式都封装进去 # -*- coding:gb2312 -*-
3Rw1y!VL%c'Z&A0import urllib2,urllib
'''
&J]o2y^7w9dd:KdM6W0函数说明:url 特殊字符编码转换
p`S6Bp\O$U0作者:xiaonan51Testing软件测试网;[Kh$^
Y4^1m
输入参数:待转换的字符串数据51Testing软件测试网p
B&\`8PyG+Q*q
mP A&w
输出参数:转换完成后的字符串数据51Testing软件测试网&kp.|6AQ6F
'''51Testing软件测试网RI"eJH.|
def urlcode(data):51Testing软件测试网prg:f ?6W7W
return urllib2.quote(str(data))
:e:\4s5H.h7o5rj0'''
4[)o7D0XERg0函数说明:获取用户信息的API接口
Q$H~6Z;`R9rc*c
^)I0作者:xiaonan
_
}$s2Y*aV0输入参数:用户名(uname),HTTP接口调用方式(GET或者POST)
2m
l!Ej"F0输出参数:HTTP接口调用返回数据51Testing软件测试网w TedQ(^
'''51Testing软件测试网XYH uu,X&k}8[
def GetUserInfo(uname,method):51Testing软件测试网TE-pO5ag7@b
if method == 'GET':51Testing软件测试网P8f1b'U%Bt
url = 'http://ip:port/interface/GetUserInfo.php?uname='+urlcode(uname)