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 ...
随机推荐
- NTKO在线office控件使用实例
目录 1. NTKO在线office控件使用实例 1.1. 基础介绍 1.2. 基本原理 1.3. 实例 1.3.1. 打开.保存部分代码 1.3.2. 动态设值 1. NTKO在线office控件使 ...
- html上传图片类型
<html> <head> <meta charset="utf-8"> <title>上传图片</title> ...
- mysql 將時間戳直接轉換成日期時間
from_unixtime()是MySQL裏的時間函數 Sql代碼 select uid,userid,username,email,FROM_UNIXTIME(addtime,'%Y年%m月%d') ...
- 【智能家居篇】wifi网络结构(下)
转载请注明出处:http://blog.csdn.net/Righthek 谢谢. 因为WIFI网络具有移动性,同一时候WIFI以无线电波作为传输媒介,这样的媒介本质上是开放的,且easy被拦截,不论 ...
- iOS开发——高级篇——iOS 强制退出程序APP代码
1.先po代码 UIAlertView* alert = [[UIAlertView alloc] initWithTitle:self.exitapplication message:@" ...
- 【bzoj1406】[AHOI2007]密码箱
x2 ≡ 1 mod n => x2 = k * n + 1 => n | (x + 1) * (x - 1) 令n = a * b,则 (a | x + 1 且 b | x - 1) 或 ...
- 学习Flash Builder编程的准备工作
1. 下载教科书,Essential ActionScript 3.0或者Programming ActionScript 3.0.这将是你的很好的老师.英语不灵的买一本中文版本. 2. 安装Flas ...
- 【IOI2013】【Bzoj3246】Dreaming
http://www.lydsy.com/JudgeOnline/problem.php?id=3246 中文题面 天地之初,世界尚在遥远的梦想之中. Serpent(水蛇)生活的地方有N个水坑,编号 ...
- ExpandableListView的首次加载全部展开,并且点击Group不收缩、
最近在做Android市场的应用.看到好多市场类的QQ应用宝做的算是最完美的了.在项目中要实现它的下载管理的实现,而界面如下: 反编译得到使用的是ExpandableListView.而怎么首次加载全 ...
- bzoj 1090 字符串折叠
题目大意: 折叠的定义如下: 1. 一个字符串可以看成它自身的折叠.2. X(S)是X(X>1)个S连接在一起的串的折叠.记作X(S)=SSSS…S(X个S). 3. 如果A=A’, B=B’, ...