python自动化--接口请求及封装
基于http协议,最常用的是GET和POST两种方法。
接口文档需要包含哪些信息:
接口名称
接口功能
接口地址
支持格式 json/xml
请求方式
请求示例
请求参数(是否必填、数据类型、传递参数格式)
返回参数说明
以典型的(一两个)参数做为判断是否请求通过(重点是看响应的信息判断)
一、GET
import requests import json url = "http://v.juhe.cn/laohuangli/d" para = {"key":"eeeeeeeeeeeeeeeeeeeeeeeeeeeeeee","date":"2017-3-22"} header ={} r = requests.get(url,params=para,headers= header,) #verify=True适用于服务端的ssl证书验证,verify=False为关闭ssl验证
print( print( print( json_r = print(json_r)
二、POST
post请求有两种请求格式:
1、key-value的格式'Content-Type':'application/x-www-form-urlencoded'
2、标准json的格式:'Content-Type':'application/json'
#key-value
import requests import json url = "http://v.juhe.cn/laohuangli/d" para = {"key":"eeeeeeeeeeeeeeeeeeeeeeeeeeeeeee","date":"2017-3-22"} header ={} r = requests.post(url,data=para,headers= header) print('get请求获取的响应结果json类型',r.text) print("get请求获取响应状态码",r.status_code) print("get请求获取响应头",r.headers['Content-Type']) #响应的json数据转换为可被python识别的数据类型 json_r = r.json() print(json_r)
#json
import requests import json url = "http://v.juhe.cn/laohuangli/d" para = {"key":"eeeeeeeeeeeeeeeeeeeeeeeeeeeeeee","date":"2017-3-22"} header ={} #python数据类型转换为json类型(json.dumps()) para = json.dumps(para) r = requests.post(url,data=para,headers= header) print('get请求获取的响应结果json类型',r.text) print("get请求获取响应状态码",r.status_code) print("get请求获取响应头",r.headers['Content-Type']) #响应的json数据转换为可被python识别的数据类型 json_r = r.json() print(json_r)
三、把所有的请求封装在函数中
def get(url,para,headers): try: r = requests.get(url,params=para,headers=headers) print("获取返回的状态码",r.status_code) json_r = r.json() print("json类型转化成python数据类型",json_r) except BaseException as e: print("请求失败!",str(e)) def post(url,para,headers): try: r = requests.post(url,data=para,headers=headers) print("获取返回的状态码",r.status_code) json_r = r.json() print("json类型转化成python数据类型",json_r) except BaseException as e: print("请求失败!",str(e)) def post_json(url,para,headers): try: data = para data = json.dumps(data) #python数据类型转化为json数据类型 r = requests.post(url,data=data,headers=headers) print("获取返回的状态码",r.status_code) json_r = r.json() print("json转换为python数据类型:",json_r) except BaseException as e: print("请求失败!",str(e)) url = "http://v.juhe.cn/laohuangli/d" para = {"key":"eeeeeeeeeeeeeeeeeeeeeeeeeeeeeee","date":"2017-3-22"} headers ={} get(url,para,headers) post(url,para,headers) post_json(url,para,headers)
四、把所有请求封装在一个对象里
class Webrequests: def get(self,url,para,headers): try: r = requests.get(url,params=para,headers=headers) print("获取返回的状态码",r.status_code) json_r = r.json() print("json类型转化成python数据类型",json_r) except BaseException as e: print("请求失败!",str(e)) def post(self,url,para,headers): try: r = requests.post(url,data=para,headers=headers) print("获取返回的状态码",r.status_code) json_r = r.json() print("json类型转化成python数据类型",json_r) except BaseException as e: print("请求失败!",str(e)) def post_json(self,url,para,headers): try: data = para data = json.dumps(data) #python数据类型转化为json数据类型 r = requests.post(url,data=data,headers=headers) print("获取返回的状态码",r.status_code) json_r = r.json() print("json类型转化成python数据类型",json_r) except BaseException as e: print("请求失败!",str(e)) url = "http://v.juhe.cn/laohuangli/d" para = {"key":"eeeeeeeeeeeeeeeeeeeeeeeeeeeeeee","date":"2017-3-22"} headers ={} q = Webrequests() q.get(url,para,headers) q.post(url,para,headers) q.post_json(url,para,headers)
python自动化--接口请求及封装的更多相关文章
- 接口测试-Java代码实现接口请求并封装
前言:在接口测试和Java开发中对接口请求方法进行封装都非常有必要,无论是在我们接口测试的时候还是在开发自测,以及调用某些第三方接口时,都能为我们调用和调试接口提供便捷: Java实现对http请求的 ...
- appium+python自动化24-滑动方法封装(swipe)
swipe介绍 1.查看源码语法,起点和终点四个坐标参数,duration是滑动屏幕持续的时间,时间越短速度越快.默认为None可不填,一般设置500-1000毫秒比较合适. swipe(self, ...
- python监控接口请求
#!/usr/bin/env python #coding=utf8 import time,os,sched,urllib,httplib import smtplib import string ...
- appium+python自动化24-滑动方法封装(swipe)【转载】
swipe介绍 1.查看源码语法,起点和终点四个坐标参数,duration是滑动屏幕持续的时间,时间越短速度越快.默认为None可不填,一般设置500-1000毫秒比较合适. swipe(self, ...
- Appium+python自动化
名称 链接地址 Appium+python自动化8-Appium Python API(上) http://mp.weixin.qq.com/s/WvpT5oRrYY22avI95FuypQ Appi ...
- vue-ajax/axios请求函数封装: axios+promise
项目文件目录/src/api ajax.js /** * ajax 请求函数模块 * 返回值为promise对象 */ import axios from 'axios' export default ...
- python接口自动化根据请求接口类型进行封装
根据不同的请求类型(GET/POST)进行接口请求封装 import requests import json class RunMain: def __init__(self, url, metho ...
- python+pytest接口自动化(11)-测试函数、测试类/测试方法的封装
前言 在python+pytest 接口自动化系列中,我们之前的文章基本都没有将代码进行封装,但实际编写自动化测试脚本中,我们都需要将测试代码进行封装,才能被测试框架识别执行. 例如单个接口的请求代码 ...
- python+pytest接口自动化(4)-requests发送get请求
python中用于请求http接口的有自带的urllib和第三方库requests,但 urllib 写法稍微有点繁琐,所以在进行接口自动化测试过程中,一般使用更为简洁且功能强大的 requests ...
随机推荐
- MongoDB小结14 - find【查询条件$lt $lte $gt $gte】
$lt $lte $gt $gte 以上四个分别表示为:< . <= . > . >= . 通常的做法是将他们组合起来,以便查找一个范围. 比如,查询年龄在18到25岁(含)的 ...
- 【CV论文阅读】Detecting events and key actors in multi-person videos
论文主要介绍一种多人协作的视频事件识别的方法,使用attention模型+RNN网络,最近粗浅地学习了RNN网络,它比较适合用于处理序列的存在上下文作用的数据. NCAA Basketball数据集 ...
- hadoop 文件操作
Create a directory in HDFS - mkdir The hadoop mkdir command is for creating directories in the hdfs. ...
- HDU-3295-An interesting mobile game(BFS+DFS)
Problem Description XQ,one of the three Sailormoon girls,is usually playing mobile games on the clas ...
- jquery 联动 年月日
<html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>JQuery实例 - 生成年 ...
- Velocity高速新手教程
变量 (1)变量的定义: #set($name = "hello") 说明:velocity中变量是弱类型的. 当使用#set 指令时,括在双引號中的字面字符串将解析和又 ...
- Python的lambda函数与排序
Python的lambda函数与排序 2010-03-02 15:02 2809人阅读 评论(0) 收藏 举报 lambdapythonlistlispclass工作 目录(?)[+] 前几天 ...
- POJ 2590:Steps
Steps Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7872 Accepted: 3612 Description ...
- 红米note手机连接mac系统
到http://www.android.com/filetransfer/ 下androidfiletransfer.dmg文件,安装好这个软件,然后再连接usb就可以用这个软件管理手机内存卡和s ...
- POJ 2977 Box walking 长方体表面两点距离
POJ2977 小学生的考试题,暴力得出O(1)的解法 #include<iostream> #include<cstdio> #include<cstdlib> ...