基于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自动化--接口请求及封装的更多相关文章

  1. 接口测试-Java代码实现接口请求并封装

    前言:在接口测试和Java开发中对接口请求方法进行封装都非常有必要,无论是在我们接口测试的时候还是在开发自测,以及调用某些第三方接口时,都能为我们调用和调试接口提供便捷: Java实现对http请求的 ...

  2. appium+python自动化24-滑动方法封装(swipe)

    swipe介绍 1.查看源码语法,起点和终点四个坐标参数,duration是滑动屏幕持续的时间,时间越短速度越快.默认为None可不填,一般设置500-1000毫秒比较合适. swipe(self, ...

  3. python监控接口请求

    #!/usr/bin/env python #coding=utf8 import time,os,sched,urllib,httplib import smtplib import string ...

  4. appium+python自动化24-滑动方法封装(swipe)【转载】

    swipe介绍 1.查看源码语法,起点和终点四个坐标参数,duration是滑动屏幕持续的时间,时间越短速度越快.默认为None可不填,一般设置500-1000毫秒比较合适. swipe(self, ...

  5. Appium+python自动化

    名称 链接地址 Appium+python自动化8-Appium Python API(上) http://mp.weixin.qq.com/s/WvpT5oRrYY22avI95FuypQ Appi ...

  6. vue-ajax/axios请求函数封装: axios+promise

    项目文件目录/src/api ajax.js /** * ajax 请求函数模块 * 返回值为promise对象 */ import axios from 'axios' export default ...

  7. python接口自动化根据请求接口类型进行封装

    根据不同的请求类型(GET/POST)进行接口请求封装 import requests import json class RunMain: def __init__(self, url, metho ...

  8. python+pytest接口自动化(11)-测试函数、测试类/测试方法的封装

    前言 在python+pytest 接口自动化系列中,我们之前的文章基本都没有将代码进行封装,但实际编写自动化测试脚本中,我们都需要将测试代码进行封装,才能被测试框架识别执行. 例如单个接口的请求代码 ...

  9. python+pytest接口自动化(4)-requests发送get请求

    python中用于请求http接口的有自带的urllib和第三方库requests,但 urllib 写法稍微有点繁琐,所以在进行接口自动化测试过程中,一般使用更为简洁且功能强大的 requests ...

随机推荐

  1. oracle常用函数积累

    --oracle常用函数积累-- --1.字符串长度:LENGTH ,语法: CONCAT(string) --示例 select LENGTH('AA_BB') from dual;--结果:5 - ...

  2. Mybatis中insert中返回主键ID的方法

    <insertid=“doSomething"parameterType="map"useGeneratedKeys="true"keyProp ...

  3. CF #367 DIV2 E

    直接使用指针,交换时交换矩阵周围的指针即可. #include <iostream> #include <cstdio> #include <cstring> us ...

  4. zabbix学习系列之基础概念

    触发器 概念 "监控项"仅负责收集数据,而通常收集数据的目的还包括在某指标对应的数据超出合理范围时给相关人员发送警告信息,"触发器"正式英语为监控项所收集的数据 ...

  5. POJ3254 状压dp

                                                                                                    Corn ...

  6. BZOJ 1037: [ZJOI2008]生日聚会Party 四维DP

    1037: [ZJOI2008]生日聚会Party Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1650  Solved: 971[Submit][ ...

  7. YTU 2639: 改错题:类中私有成员的访问

    2639: 改错题:类中私有成员的访问 时间限制: 1 Sec  内存限制: 128 MB 提交: 431  解决: 297 题目描述 /* 改错题: 设计一个日期类和时间类,并编写全局函数displ ...

  8. 记录一次Mysql死锁排查过程

    背景 以前接触到的数据库死锁,都是批量更新时加锁顺序不一致而导致的死锁,但是上周却遇到了一个很难理解的死锁.借着这个机会又重新学习了一下mysql的死锁知识以及常见的死锁场景.在多方调研以及和同事们的 ...

  9. Java序列化系列教程(上)

    一定义以及相关概念 互联网的产生带来了机器间通讯的需求,而互联通讯的双方需要采用约定的协议,序列化和反序列化属于通讯协议的一部分.通讯协议往往采用分层模型,不同模型每层的功能定义以及颗粒度不同,例如: ...

  10. Centos7 配置防火墙 firewall

    一.firewall 1.从CentOS7开始,默认使用firewall来配置防火墙,没有安装iptables(旧版默认安装). 2.firewall的配置文件是以xml的格式,存储在 /usr/li ...