五、Python编写接口自动化
1. 总结结果
2. 详细代码:
# -*- coding: utf-8 -*- import requests import json class web_requests(object): def __init__(self): pass def Interface(self,Interface_path,**My_data): url = "http://testdj.XXXXXX.com:8378/%s" %(Interface_path) # 测试的接口url headers = {"Host": "testdj.XXXXXX.com:8378", "Accept": "application/json, text/plain, */*", "Referer": "http://testdj.XXXXXX.com:8280/", "Accept-Language": "zh-CN,zh;q=0.8", "Origin": "http://testdj.XXXXXX.com:8280", "Accept-Encoding": "gzip, deflate", "Accept": "application/json, text/plain, */*", "User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.79 Safari/537.36"} #消息头 #data1 = {"beginDate": "2018-01-01", "endDate": "2018-04-01"} # 接口传送的参数 data = My_data # 接口传送的参数 r = requests.get(url=url, json=data, headers=headers) # 发送请求 # return r.json print (r.text) # 获取响应报文 print (r.status_code) a = web_requests() a.Interface('monitor/sxsb-sxzl',beginDate="2018-01-01", endDate="2018-04-01")
3. 代码说明:
1、接口的URL:
url = "http://testdj.XXXXXX.com:8378/%s"%(Interface_path)
2、请求消息头:
headers = {"Host": "testdj.XXXXXX.com:8378",
"Accept": "application/json,text/plain, */*",
"Referer": "http://testdj.XXXXXX.com:8280/",
"Accept-Language": "zh-CN,zh;q=0.8",
"Origin": "http://testdj.XXXXXX.com:8280",
"Accept-Encoding": "gzip,deflate",
"Accept": "application/json,text/plain, */*",
"User-Agent": "Mozilla/5.0(Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)Chrome/61.0.3163.79 Safari/537.36"} #消息头
3、接口的传参数:
data = My_data #接口传送的参数
注:
python传参数的格式原本如下:
data ={"beginDate": "2018-01-01", "endDate":"2018-04-01"} # 接口传送的参数
但为了方便给外部引用进来,做了如下特殊处理:
这里存在多个参数
a.Interface('monitor/sxsb-sxzl',beginDate="2018-01-01",endDate="2018-04-01")
python将多个参数合并,重点是“**”
def Interface(self,Interface_path,**My_data):
调用时可以直接写:
data = My_data #接口传送的参数
相当于:data = {'beginDate':'2018-01-01', 'endDate': '2018-04-01'}
4、给服务器发起请求:
r = requests.get(url=url, json=data, headers=headers) # 发送请求
5、服务器返回结果:
print (r.text) # 获取响应报文
4. Robot引用:
(1)、py代码
Py文件名:Web_Interface.py
代码如下:
# -*- coding: utf-8 -*-
import requests
import json
class Web_Interface(object):
def __init__(self):
pass
def web_interface(self,Interface_path,**My_data):
url = "http://testdj.XXXXXX.com:8378%s" %(Interface_path)# 测试的接口url
headers = {"Host": "testdj.XXXXXX.com:8378",
"Accept":"application/json, text/plain, */*",
"Referer":"http://testdj.XXXXXX.com:8280/",
"Accept-Language": "zh-CN,zh;q=0.8",
"Origin":"http://testdj.XXXXXX.com:8280",
"Accept-Encoding": "gzip, deflate",
"Accept":"application/json, text/plain, */*",
"User-Agent":"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, likeGecko) Chrome/61.0.3163.79 Safari/537.36"} #消息头
#data1 = {"beginDate": "2018-01-01","endDate": "2018-04-01"} # 接口传送的参数
data = My_data # 接口传送的参数
r = requests.get(url=url, json=data, headers=headers) # 发送请求
#print (r.text) # 获取响应报文
#print (r.status_code) # 获取结果码
return json.loads(r.text)
(2)、引用py脚本
(3)、robot使用
(4)输出结果
5. 参见问题:
(1)、python接口能返回结果,但在获取结果里的data值却报错了(string indices must be integers)
原因是接口返回结果没有转换转成obj
原代码:
return r.text
用json.loads 修复如下:
return json.loads(r.text)