python自动化--接口请求及封装
基于http协议,最常用的是GET和POST两种方法。
接口文档需要包含哪些信息:
接口名称
接口功能
接口地址
支持格式 json/xml
请求方式
请求示例
请求参数(是否必填、数据类型、传递参数格式)
返回参数说明
以典型的(一两个)参数做为判断是否请求通过(重点是看响应的信息判断)
一、GET
1 import requests 2 import json 3 4 url = "http://v.juhe.cn/laohuangli/d" 5 para = {"key":"eeeeeeeeeeeeeeeeeeeeeeeeeeeeeee","date":"2017-3-22"} 6 header ={} 7 8 r = requests.get(url,params=para,headers= header,) 9 #verify=True适用于服务端的ssl证书验证,verify=False为关闭ssl验证
10 print('get请求获取的响应结果json类型',r.text) 11 print("get请求获取响应状态码",r.status_code) 12 print("get请求获取响应头",r.headers['Content-Type']) 13 14 #响应的json数据转换为可被python识别的数据类型 15 json_r = r.json() 16 print(json_r)
二、POST
post请求有两种请求格式:
1、key-value的格式'Content-Type':'application/x-www-form-urlencoded'
2、标准json的格式:'Content-Type':'application/json'
#key-value
1 import requests 2 import json 3 4 url = "http://v.juhe.cn/laohuangli/d" 5 para = {"key":"eeeeeeeeeeeeeeeeeeeeeeeeeeeeeee","date":"2017-3-22"} 6 header ={} 7 8 r = requests.post(url,data=para,headers= header) 9 10 print('get请求获取的响应结果json类型',r.text) 11 print("get请求获取响应状态码",r.status_code) 12 print("get请求获取响应头",r.headers['Content-Type']) 13 14 #响应的json数据转换为可被python识别的数据类型 15 json_r = r.json() 16 print(json_r)
#json
1 import requests 2 import json 3 4 url = "http://v.juhe.cn/laohuangli/d" 5 para = {"key":"eeeeeeeeeeeeeeeeeeeeeeeeeeeeeee","date":"2017-3-22"} 6 header ={} 7 #python数据类型转换为json类型(json.dumps()) 8 para = json.dumps(para) 9 r = requests.post(url,data=para,headers= header) 10 11 print('get请求获取的响应结果json类型',r.text) 12 print("get请求获取响应状态码",r.status_code) 13 print("get请求获取响应头",r.headers['Content-Type']) 14 15 #响应的json数据转换为可被python识别的数据类型 16 json_r = r.json() 17 print(json_r)
三、把所有的请求封装在函数中
1 def get(url,para,headers): 2 try: 3 r = requests.get(url,params=para,headers=headers) 4 print("获取返回的状态码",r.status_code) 5 json_r = r.json() 6 print("json类型转化成python数据类型",json_r) 7 except BaseException as e: 8 print("请求失败!",str(e)) 9 def post(url,para,headers): 10 try: 11 r = requests.post(url,data=para,headers=headers) 12 print("获取返回的状态码",r.status_code) 13 json_r = r.json() 14 print("json类型转化成python数据类型",json_r) 15 except BaseException as e: 16 print("请求失败!",str(e)) 17 def post_json(url,para,headers): 18 try: 19 data = para 20 data = json.dumps(data) #python数据类型转化为json数据类型 21 r = requests.post(url,data=data,headers=headers) 22 print("获取返回的状态码",r.status_code) 23 json_r = r.json() 24 print("json转换为python数据类型:",json_r) 25 except BaseException as e: 26 print("请求失败!",str(e)) 27 28 url = "http://v.juhe.cn/laohuangli/d" 29 para = {"key":"eeeeeeeeeeeeeeeeeeeeeeeeeeeeeee","date":"2017-3-22"} 30 headers ={} 31 32 get(url,para,headers) 33 post(url,para,headers) 34 post_json(url,para,headers)
四、把所有请求封装在一个对象里
1 class Webrequests: 2 def get(self,url,para,headers): 3 try: 4 r = requests.get(url,params=para,headers=headers) 5 print("获取返回的状态码",r.status_code) 6 json_r = r.json() 7 print("json类型转化成python数据类型",json_r) 8 except BaseException as e: 9 print("请求失败!",str(e)) 10 def post(self,url,para,headers): 11 try: 12 r = requests.post(url,data=para,headers=headers) 13 print("获取返回的状态码",r.status_code) 14 json_r = r.json() 15 print("json类型转化成python数据类型",json_r) 16 except BaseException as e: 17 print("请求失败!",str(e)) 18 def post_json(self,url,para,headers): 19 try: 20 data = para 21 data = json.dumps(data) #python数据类型转化为json数据类型 22 r = requests.post(url,data=data,headers=headers) 23 print("获取返回的状态码",r.status_code) 24 json_r = r.json() 25 print("json类型转化成python数据类型",json_r) 26 except BaseException as e: 27 print("请求失败!",str(e)) 28 29 url = "http://v.juhe.cn/laohuangli/d" 30 para = {"key":"eeeeeeeeeeeeeeeeeeeeeeeeeeeeeee","date":"2017-3-22"} 31 headers ={} 32 33 q = Webrequests() 34 35 q.get(url,para,headers) 36 q.post(url,para,headers) 37 q.post_json(url,para,headers)
转载于:https://www.cnblogs.com/hanxiaobei/p/6613968.html
python自动化--接口请求及封装相关推荐
- python 接口测试 url_Python 接口测试之接口请求方法封装
引言 前面讲过三篇文章: 既然我们接口测试用例写好了,测试数据也拿到了,那么就是模拟调用接口的方法了,方法有get,post,put,delete,具体是选择哪种,我们需要根据不同接口规定好请求方法来 ...
- python自动化接口_Python接口自动化——Web接口
原标题:Python接口自动化--Web接口 1.2.1 web接口的概念 这里用一个浏览器调试工具捕捉课程管理页面请求作为例子: 当请求页面时,服务器会返回资源,将协议看做是路的话,http可以看做 ...
- python滑动手机屏幕_appium+python自动化24-滑动方法封装(swipe)
swipe介绍 1.查看源码语法,起点和终点四个坐标参数,duration是滑动屏幕持续的时间,时间越短速度越快.默认为None可不填,一般设置500-1000毫秒比较合适. swipe(self, ...
- 接口自动化-接口请求数据准备-如何生成随机姓名、年龄、号码、email等
在做接口自动化测试的时候,经常需要一些随机的测试数据,这里提供一个个人使用的生成随机值的工具类,主要内容都已在代码中注释: package com.gvbrain.appApi.Utils;impor ...
- 〖Python接口自动化测试实战篇⑧〗- 小案例 - 使用python实现接口请求 [查询天行数据]
点击下方图片有惊喜,精美礼品等你来拿哦 前言: ✌ 作者简介:渴望力量的哈士奇 ✌,大家可以叫我
- python实现http请求并发_Python使用grequests并发发送请求
前言 requests是Python发送接口请求非常好用的一个三方库,由K神编写,简单,方便上手快.但是requests发送请求是串行的,即阻塞的.发送完一条请求才能发送另一条请求. 为了提升测试效率 ...
- Python接口自动化实战 ( 第一阶段) - 封装接口请求类和异常处理
1.封装http接口请求 已经实现了一个简单的接口请求,接下来就要考虑封装这个请求,在后面的用例中,只需要传递参数(URL ,Params,cookie,heade,method 等)每次去调用这个请 ...
- python接口自动化根据请求接口类型进行封装
根据不同的请求类型(GET/POST)进行接口请求封装 1 import requests 2 import json 3 4 5 class RunMain: 6 7 def __init__(se ...
- python+pytest接口自动化之测试函数、测试类/测试方法的封装
前言 今天呢,笔者想和大家聊聊python+pytest接口自动化中将代码进行封装,只有将测试代码进行封装,才能被测试框架识别执行. 例如单个接口的请求代码如下: import requestshea ...
- Python自动化学习笔记(八)——接口开发、发送网络请求、发送邮件、写日志...
1.接口开发(flask模块) Python自动化学习笔记(七)接口开发部分的内容补充 1.1参数为json格式: flask.request.is_json #判断参数是否是json格式 flask ...
最新文章
- Qt pro文件下跨平台宏的使用(windows/linux 以及x86 和 arm的区分)
- teams怎么去看谁在说话
- spring中的spel表达式语言
- python中import os_Python常用模块os--与操作系统交互
- 诺基亚AirScale支持低频段和高频段5G服务 确保运营商投资收入
- nfsd linux是什么进程,Linux 下的 NFS 系统简介
- python 矩阵类型转换_python中的矩阵运算
- Python机器学习:KNN算法07数据归一化
- C++之构造函数、(构造)函数重载探究
- vue.3.0 dom赋值_Vue 3.0 快速入门
- lvs + keepalived + nginx 实现高可用
- java终止程序语句总结 System.exit(1)、System.exit(0)、return;break;continue;
- C# WinForm开发系列 - ComboBox
- 量子计算机 时间倒流,科学家首次利用量子计算机成功逆转时间,时间倒流将变成可能?...
- 计算机版音乐教学,EduOffice音乐教学备课软件
- java判断字母是否为元音_Java程序来检查字母是元音还是辅音
- c语言水果店信息管理系统,C语言排序(1)___水果销售
- 51Nod——T 1631 小鲨鱼在51nod小学
- Visual Studio2015安装步骤详解
- 二元函数可微与可导的关系_函数连续、可导与可微之间的关系