浅谈Python接口对json串的处理方法


Posted in Python onDecember 19, 2018

最近学习Python接口测试,对于接口测试完全小白。大概一周的学习成果进行总结。

1.接口测试:

目前涉及到的只是对简单单一的接口进行参数传递,得到返回自。

2.关于各种概念:

2.1 http请求包含post方法、get方法。通过json串或XML传递,但后者未做研究

2.2 GET: 浏览器告诉服务器,只获取页面信息,并发送给我。

2.3 POST:浏览器告诉服务器想法不一些信息到某个网址,服务器需确保数据被存储且只存储一次。

2.4 HEAD:浏览器告诉服务器,给我消息头,像get那样被接收。

2.5 Python对数据的处理模块可以使用urllib、urllib2模块或requests模块

3.urllib、urllib2实例

#coding=utf_8
import urllib2,urllib
import json
import unittest,time,re

class APITest():
  """
  接口测试类
  """
  def api_test(self, method, url, getparams, postparams):
    str1 = ''

    #GET方法调用
    if method == 'GET':
      if getparams != "":
        for x in getparams:
          str1 = str1 + x + '=' + urllib2.quote(str(getparams.get(x)))
          if len(getparams) > 2:
            str1 = str1 + "&"
        url = url + "&" + str1

      result = urllib2.urlopen(url).read()

    #POST方法调用
    if method=='POST':
      if postparams != "":
        data = urllib.urlencode(postparams)
        req = urllib2.Request(data)
      response = urllib2.urlopen(req)
      result = response.read()

    #result转为json数据
    jsdata = json.loads(result)
    return jsdata

class APIGetRes(unittest.TestCase):
  def test_call(self):
    api = APITest()
    getparams={'keyword':'测试'}
    postparams=''
    data = api.api_test('GET','http://api.zhongchou.cn/deal/list?v=1',getparams,postparams)
    print data
    if (data['errno']!=""):
      self.assertEqual(0, data['errno'])
      print"接口 deal/list-------------OK!"
    else:
      print"接口 deal/list-------------Failure!"
      self.assertEqual(0, data['errno'])

if __name__ == '__main__':
  unittest.main()

Requests实例

#coding=utf_8
import requests
import json
import unittest,time,re


class APIGetAdlis(unittest.TestCase):
  def test_call(self):
    github_url='http://api.zhongchou.cn/deal/list?v=1'
    data = json.dumps({'keyword':'测试'})
    resp = requests.post(github_url,data)
    print resp.json
    #if (data['errno']!=''):
    #  self.assertEqual(0, data['errno'])
    #  print"接口 deal/list-------------OK!"
    #else:
    #  print"接口 deal/list-------------Failure!"
    #  self.assertEqual(0, data['errno'])

粗略了解,待深入学习!

以上这篇浅谈Python接口对json串的处理方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
PyQt5每天必学之布局管理
Apr 19 Python
pycharm设置注释颜色的方法
May 23 Python
使用python批量化音乐文件格式转换的实例
Jan 09 Python
python操作日志的封装方法(两种方法)
May 23 Python
Python虚拟环境的原理及使用详解
Jul 02 Python
Numpy 中的矩阵求逆实例
Aug 26 Python
python动态视频下载器的实现方法
Sep 16 Python
安装2019Pycharm最新版本的教程详解
Oct 22 Python
Pyorch之numpy与torch之间相互转换方式
Dec 31 Python
如何用python 操作zookeeper
Dec 28 Python
python装饰器代码深入讲解
Mar 01 Python
Python答题卡识别并给出分数的实现代码
Jun 22 Python
python实现的MySQL增删改查操作实例小结
Dec 19 #Python
python3 http提交json参数并获取返回值的方法
Dec 19 #Python
python3.6使用urllib完成下载的实例
Dec 19 #Python
使用urllib库的urlretrieve()方法下载网络文件到本地的方法
Dec 19 #Python
对python内置map和six.moves.map的区别详解
Dec 19 #Python
对python中的six.moves模块的下载函数urlretrieve详解
Dec 19 #Python
python爬虫URL重试机制的实现方法(python2.7以及python3.5)
Dec 18 #Python
You might like
php中记录用户访问过的产品,在cookie记录产品id,id取得产品信息
2011/05/04 PHP
解析phpstorm + xdebug 远程断点调试
2013/06/20 PHP
ThinkPHP中的常用查询语言汇总
2014/08/22 PHP
php提取字符串中网站url地址的方法
2014/12/03 PHP
你应该知道PHP浮点数知识
2015/05/13 PHP
用php定义一个数组最简单的方法
2019/10/04 PHP
CentOS7系统搭建LAMP及更新PHP版本操作详解
2020/03/26 PHP
php自动加载代码实例详解
2021/02/26 PHP
动态添加js事件实现代码
2009/03/12 Javascript
jquery实现兼容浏览器的图片上传本地预览功能
2013/10/14 Javascript
JavaScript检测鼠标移动方向的方法
2015/05/22 Javascript
js仿手机页面文件下拉刷新效果
2016/10/14 Javascript
bootstrap为水平排列的表单和内联表单设置可选的图标
2017/02/15 Javascript
Angularjs+bootstrap+table多选(全选)支持单击行选中实现编辑、删除功能
2017/03/27 Javascript
Vue学习笔记进阶篇之vue-cli安装及介绍
2017/07/18 Javascript
使用SVG基本操作API的实例讲解
2017/09/14 Javascript
layui实现多图片上传并限制上传的图片数量
2019/09/26 Javascript
JavaScript布尔运算符原理使用解析
2020/05/06 Javascript
nuxt 自定义 auth 中间件实现令牌的持久化操作
2020/11/05 Javascript
[01:05:00]2018国际邀请赛 表演赛 Pain vs OpenAI
2018/08/24 DOTA
使用python读取txt文件的内容,并删除重复的行数方法
2018/04/18 Python
Python中pandas dataframe删除一行或一列:drop函数详解
2018/07/03 Python
python爬虫之urllib,伪装,超时设置,异常处理的方法
2018/12/19 Python
tensorflow如何批量读取图片
2019/08/29 Python
解决pyshp UnicodeDecodeError的问题
2019/12/06 Python
基于Python的接口自动化读写excel文件的方法
2021/01/15 Python
StubHub希腊:购买体育赛事、音乐会和剧院门票
2019/08/03 全球购物
歌颂祖国演讲稿
2014/05/04 职场文书
赞美老师的演讲稿
2014/05/22 职场文书
科级干部群众路线教育实践活动个人对照检查材料
2014/09/19 职场文书
校园文化艺术节宣传标语
2014/10/09 职场文书
车间主任岗位职责范本
2015/04/08 职场文书
公司员工手册范本
2015/05/14 职场文书
【2·13】一图读懂中国无线电发展
2022/02/18 无线电
面试中canvas绘制图片模糊图片问题处理
2022/03/13 Javascript
Java由浅入深通关抽象类与接口(下篇)
2022/04/26 Java/Android