国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > Python > 正文

淺談Python接口對(duì)json串的處理方法

2020-02-16 00:12:56
字體:
供稿:網(wǎng)友

最近學(xué)習(xí)Python接口測(cè)試,對(duì)于接口測(cè)試完全小白。大概一周的學(xué)習(xí)成果進(jìn)行總結(jié)。

1.接口測(cè)試:

目前涉及到的只是對(duì)簡(jiǎn)單單一的接口進(jìn)行參數(shù)傳遞,得到返回自。

2.關(guān)于各種概念:

2.1 http請(qǐng)求包含post方法、get方法。通過json串或XML傳遞,但后者未做研究

2.2 GET: 瀏覽器告訴服務(wù)器,只獲取頁(yè)面信息,并發(fā)送給我。

2.3 POST:瀏覽器告訴服務(wù)器想法不一些信息到某個(gè)網(wǎng)址,服務(wù)器需確保數(shù)據(jù)被存儲(chǔ)且只存儲(chǔ)一次。

2.4 HEAD:瀏覽器告訴服務(wù)器,給我消息頭,像get那樣被接收。

2.5 Python對(duì)數(shù)據(jù)的處理模塊可以使用urllib、urllib2模塊或requests模塊

3.urllib、urllib2實(shí)例

#coding=utf_8import urllib2,urllibimport jsonimport unittest,time,reclass APITest():  """  接口測(cè)試類  """  def api_test(self, method, url, getparams, postparams):    str1 = ''    #GET方法調(diào)用    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方法調(diào)用    if method=='POST':      if postparams != "":        data = urllib.urlencode(postparams)        req = urllib2.Request(data)      response = urllib2.urlopen(req)      result = response.read()    #result轉(zhuǎn)為json數(shù)據(jù)    jsdata = json.loads(result)    return jsdataclass APIGetRes(unittest.TestCase):  def test_call(self):    api = APITest()    getparams={'keyword':'測(cè)試'}    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實(shí)例

#coding=utf_8import requestsimport jsonimport unittest,time,reclass APIGetAdlis(unittest.TestCase):  def test_call(self):    github_url='http://api.zhongchou.cn/deal/list?v=1'    data = json.dumps({'keyword':'測(cè)試'})    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'])

粗略了解,待深入學(xué)習(xí)!

以上這篇淺談Python接口對(duì)json串的處理方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持武林站長(zhǎng)站。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 石柱| 斗六市| 行唐县| 绥德县| 天峨县| 无棣县| 鹿泉市| 航空| 常州市| 东平县| 绥宁县| 邻水| 沅陵县| 济阳县| 淮北市| 灯塔市| 铅山县| 隆德县| 建阳市| 日喀则市| 五莲县| 宝兴县| 博湖县| 遂溪县| 韶关市| 行唐县| 舟曲县| 基隆市| 长治县| 诸暨市| 桐柏县| 丹东市| 瓦房店市| 柞水县| 焦作市| 于田县| 泰州市| 洪泽县| 大余县| 淄博市| 泰州市|