python3 接口测试 一般方法

第一步: 导入相关包
import http.client

第二步: 将ip和端口号,使用如下命令,赋值给变量.
conn = http.client.HTTPConnection("124.193.90.194:8020")
第三步: 将要传递的参数,使用&符号拼接起来,并赋值给变量
payload = "organizationNumber=91&consumerCode=B00000010&accountCode=000000031"
第四步: 将访问要传递的头信息,赋值给变量headers.
headers = {
'accept': "text/css,*/*;q=0.1",
'accept-encoding': "gzip, deflate, sdch",
'accept-language': "zh-CN,zh;q=0.8",
'cache-control': "no-cache",
'connection': "keep-alive",
'cookie': "SHIROSESSIONID=e4964d74-89d0-4afa-8127-c7a0ca4749bd; loginName=admin; loginTime=Wed%20May%2010%202017%2012%3A51%3A24%20GMT%2B0800%20(%E4%B8%AD%E5%9B%BD%E6%A0%87%E5%87%86%E6%97%B6%E9%97%B4); source=PT; coId=",
'host': "124.193.90.194:8020",
'pragma': "no-cache",
'referer': "http://124.193.90.194:8020/index.html",
'user-agent': "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36",
'postman-token': "ad36236b-b57d-61c7-6f8c-9a52d631c87a",
'content-type': "application/x-www-form-urlencoded"
}
第五步: 使用如下的request命令,进行接口的访问,进行接口测试.
conn.request("POST", "/cams/api/accountQuery", payload, headers)
第六步: 获取接口测试的响应,并赋值给变量,
res = conn.getresponse()
第七步: 使用read()方法读取出来,并赋值给变量
data = res.read()
第八步: 将获取的响应打印出来.
print(data.decode("utf-8")

posted @ 2017-05-12 13:46  心悦飞飞  阅读(431)  评论(0编辑  收藏  举报