浅谈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 相关文章推荐
python实现根据窗口标题调用窗口的方法
Mar 13 Python
Python中线程的MQ消息队列实现以及消息队列的优点解析
Jun 29 Python
基于Django的ModelForm组件(详解)
Dec 07 Python
VScode编写第一个Python程序HelloWorld步骤
Apr 06 Python
对IPython交互模式下的退出方法详解
Feb 16 Python
pytorch 共享参数的示例
Aug 17 Python
TensorFlow tf.nn.max_pool实现池化操作方式
Jan 04 Python
python将音频进行变速的操作方法
Apr 08 Python
Pycharm pyuic5实现将ui文件转为py文件,让UI界面成功显示
Apr 08 Python
Python 爬虫的原理
Jul 30 Python
Python调用C/C++的方法解析
Aug 05 Python
Pandas替换及部分替换(replace)实现流程详解
Oct 12 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
虹吸式咖啡探讨–研磨
2021/03/03 冲泡冲煮
PHP+jQuery 注册模块的改进(一):验证码存入SESSION
2014/10/14 PHP
PHP设计模式之注册树模式分析
2018/01/26 PHP
gearman中worker常驻后台,导致MySQL server has gone away的解决方法
2020/02/27 PHP
Laravel框架使用技巧之使用url()全局函数返回前一个页面的地址方法详解
2020/04/06 PHP
JavaScript RegExp方法获取地址栏参数(面向对象)
2009/03/10 Javascript
jQuery库与其他JS库冲突的解决办法
2010/02/07 Javascript
jQuery对html元素取值与赋值的方法
2013/11/20 Javascript
JS短信验证码倒计时功能的实现(没有验证码,只有倒计时)
2016/10/27 Javascript
Nodejs下DNS缓存问题浅析
2016/11/16 NodeJs
微信小程序开发探究
2016/12/27 Javascript
RequireJs的使用详解
2017/02/19 Javascript
详解JS中的this、apply、call、bind(经典面试题)
2017/09/19 Javascript
JavaScript获取用户所在城市及地理位置
2018/04/21 Javascript
[06:16]《DAC最前线》之地区预选赛全面回顾
2015/01/19 DOTA
详解Python编程中对Monkey Patch猴子补丁开发方式的运用
2016/05/27 Python
python实现从pdf文件中提取文本,并自动翻译的方法
2018/11/28 Python
Python常见数据类型转换操作示例
2019/05/08 Python
Python的垃圾回收机制详解
2019/08/28 Python
python 3.74 运行import numpy as np 报错lib\site-packages\numpy\__init__.py
2019/10/06 Python
Python 静态方法和类方法实例分析
2019/11/21 Python
六种酷炫Python运行进度条效果的实现代码
2020/07/17 Python
python实现猜拳游戏项目
2020/11/30 Python
Michael Kors美国官网:美式奢侈生活风格的代表
2016/11/25 全球购物
WiFi云数码相框:Nixplay
2018/07/05 全球购物
Hotels.com韩国:海外国内旅行所需的酒店和住宿预订网站
2020/05/08 全球购物
JSF界面控制层技术
2013/06/17 面试题
三年级语文教学反思
2014/02/01 职场文书
事业单位考核材料
2014/05/21 职场文书
法院授权委托书范文
2014/08/02 职场文书
2014年十一国庆节活动方案
2014/09/16 职场文书
销售经理工作检讨书
2015/02/19 职场文书
mysql5.7使用binlog 恢复数据的方法
2021/06/03 MySQL
淡雅古典唯美少女娇媚宁静迷人写真
2022/03/21 杂记
Kubernetes中Deployment的升级与回滚
2022/04/01 Servers
Apache SeaTunnel实现 非CDC数据抽取
2022/05/20 Servers