基于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. Oracle442个应用场景---------PL/SQL基础

    ----------------------------------------------------------------------------------- 备份和恢复数据库略过.在后面解说 ...

  2. 字符串处理(正则表达式、NSScanner扫描、CoreParse解析器)-b

    搜索 在一个字符串中搜索子字符串 最灵活的方法 1 - (NSRange)rangeOfString:(NSString *)aString options:(NSStringCompareOptio ...

  3. USRP内部的寄存器

    usrp_regs.hpp #ifndef INCLUDED_USRP2_REGS_HPP #define INCLUDED_USRP2_REGS_HPP ////////////////////// ...

  4. HDU1236 排名 题解

    Problem Description 今天的上机考试尽管有实时的Ranklist,但上面的排名仅仅是依据完毕的题数排序,没有考虑  每题的分值,所以并非最后的排名.给定录取分数线.请你敲代码找出最后 ...

  5. ABAP OLE

    OLE DATA: excel TYPE ole2_object, workbook TYPE ole2_object, sheet TYPE ole2_object, cell TYPE ole2_ ...

  6. iOS方法重写

    在O-C中子类可以继承父类的方法 ,而不需要从新编写相同的方法,但是有有时候子类并不想原封不动的继承父类的方法,而且是想做一些修改,这就采用啦方法的重写,方法从写有叫做方法覆盖,若子类的中的方法与父类 ...

  7. CentOS常用基础命令大全

    这篇文章主要介绍了CentOS常用基础命令大全,学习centos的朋友需要掌握的知识,需要的朋友可以参考下 1.关机 (系统的关机.重启以及登出 ) 的命令shutdown -h now 关闭系统(1 ...

  8. Java 基础 —— enum

    枚举的遍历: enum Suit { CLUB, DIAMOND, HEART, SPADE } Collection<Suit> suitTypes = Arrays.asList(Su ...

  9. go语言---传值和传引用

    go语言---传值和传引用 https://blog.csdn.net/cyk2396/article/details/78893828 1.定义: b = a; b.modify(); 如果b的修改 ...

  10. Flask的配置文件 与 session

    配置文件 flask中的配置文件是一个flask.config.Config对象(继承字典) 默认配置为: { 'DEBUG': get_debug_flag(default=False), 是否开启 ...