基于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. hbase definitive guide 笔记

    ext3 file system 优化 ext3 在用在hbase上可以做如下优化: 1. mount的时候加上noatime选项.这可以减少管理开销 2. 用命令tune2fs -m 0 /dev/ ...

  2. hadoop2.2集群搭建问题只能启动一个datanode问题

    按照教程http://cn.soulmachine.me/blog/20140205/搭建总是出现如下问题: 2014-04-13 23:53:45,450 INFO org.apache.hadoo ...

  3. 链表倒置,这个还是考验仔细程度,第一遍还没做对 —— 剑指Offer

    https://www.nowcoder.net/practice/75e878df47f24fdc9dc3e400ec6058ca?tpId=13&tqId=11168&tPage= ...

  4. Centos7 samba 匿名共享 简单config

    安装Samba yum install samba samba-client samba-common -y 备份原始的Samba配置文件: mv /etc/samba/smb.conf /etc/s ...

  5. Yarn 的工作流-创建一个新项目

    Microsoft Windows [版本 10.0.16299.125] (c) Microsoft Corporation.保留所有权利. C:\Users\Administrator>cd ...

  6. 协方差矩阵与主成分分析PCA

    今天看论文,作者是用主成分分析(PCA)的方法做的.仔细学习了一下,有一篇博客写的很好,介绍的深入浅出! 协方差:http://pinkyjie.com/2010/08/31/covariance/ ...

  7. jboss相关的术语

    1 jboss eap java ee application server.red hat官方版本. 2 jboss as/wildfly java ee application server的社区 ...

  8. js执行“按回车”的动作

    <textarea class="W_input" style="overflow: hidden; height: 23px;" node-type=& ...

  9. 【CEOI2002】【Poj 1038】Bugs Integrated, Inc.

    http://poj.org/problem?id=1038 发一下中文题面(今天考试直接被改了): 生记茶餐厅由于受杀人事件的影响,生意日渐冷清,不得不暂时歇业.四喜赋闲在家,整天抱着零食看电视,在 ...

  10. 【HDU 4547】 CD操作

    [题目链接] 点击打开链接 [算法] 分四种情况讨论 : 1. 当前目录和目标目录是同一目录,不需要变换,答案为0 2. 当前目录是目标目录的祖先,答案为当前目录的深度 - 目标目录的深度 3. 当前 ...