浅谈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中常用的各种数据库操作模块和连接实例
May 29 Python
python循环监控远程端口的方法
Mar 14 Python
Python的动态重新封装的教程
Apr 11 Python
python 字典(dict)按键和值排序
Jun 28 Python
Python与Java间Socket通信实例代码
Mar 06 Python
pycharm安装图文教程
May 02 Python
python实现日常记账本小程序
Mar 10 Python
详解Django CAS 解决方案
Oct 30 Python
详解Python修复遥感影像条带的两种方式
Feb 23 Python
Python基础之列表常见操作经典实例详解
Feb 26 Python
40行Python代码实现天气预报和每日鸡汤推送功能
Feb 27 Python
基于Python正确读取资源文件
Sep 14 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 如何获取数组第一个值
2013/08/06 PHP
php中file_get_content 和curl以及fopen 效率分析
2014/09/19 PHP
php实现获取文件mime类型的方法
2015/02/11 PHP
php编译安装php-amq扩展简明教程
2016/06/25 PHP
深入分析PHP优化及注意事项
2016/07/04 PHP
php each 返回数组中当前的键值对并将数组指针向前移动一步实例
2016/11/22 PHP
php往mysql中批量插入数据实例教程
2018/12/12 PHP
php获取是星期几的的一些常用姿势
2019/12/15 PHP
javascript 动态添加事件代码
2008/11/30 Javascript
js Date自定义函数 延迟脚本执行
2010/03/10 Javascript
js location.replace与location.reload的区别
2010/09/08 Javascript
JavaScript call apply使用 JavaScript对象的方法绑定到DOM事件后this指向问题
2011/09/28 Javascript
一个页面放2段图片滚动代码出现冲突的问题如何解决
2012/12/21 Javascript
JavaScript中的undefined学习总结
2013/11/30 Javascript
jQuery中next()方法用法实例
2015/01/07 Javascript
JavaScript表格常用操作方法汇总
2015/04/15 Javascript
Angularjs自定义指令实现三级联动 选择地理位置
2017/02/13 Javascript
Nodejs搭建wss服务器教程
2017/05/24 NodeJs
vue toggle做一个点击切换class(实例讲解)
2018/03/13 Javascript
JavaScript面试出现频繁的一些易错点整理
2018/03/29 Javascript
解决vue 给window添加和移除resize事件遇到的坑
2020/07/21 Javascript
Python中使用wxPython开发的一个简易笔记本程序实例
2015/02/08 Python
Python中一些自然语言工具的使用的入门教程
2015/04/13 Python
Python学习小技巧之列表项的推导式与过滤操作
2017/05/20 Python
对Python进行数据分析_关于Package的安装问题
2017/05/22 Python
Python3 单行多行万能正则匹配方法
2019/01/07 Python
使用Django搭建一个基金模拟交易系统教程
2019/11/18 Python
python将dict中的unicode打印成中文实例
2020/05/11 Python
英国国家美术馆商店:National Gallery
2019/05/01 全球购物
C语言面试题
2015/10/30 面试题
个性发展自我评价
2014/02/11 职场文书
机械设计制造及其自动化专业求职信
2014/06/17 职场文书
最美家庭活动方案
2014/08/31 职场文书
python3操作redis实现List列表实例
2021/08/04 Python
nginx中封禁ip和允许内网ip访问的实现示例
2022/03/17 Servers
了解Kubernetes中的Service和Endpoint
2022/04/01 Servers