qileilove

          blog已經(jīng)轉(zhuǎn)移至github,大家請訪問 http://qaseven.github.io/

          Python優(yōu)雅的Requests庫助力性能測試

            Python’s standard urllib2 module provides most of the HTTP capabilities you need, but the API is thoroughly broken. It was built for a different time — and a different web. It requires an enormous amount of work (even method overrides) to perform the simplest of tasks.
            Things shouldn’t be this way. Not in Python.
            是的,Python的urllib2不應(yīng)該是這樣,當(dāng)我們試圖讓http庫更加優(yōu)雅的時候,我們找到了Requests,有一種相見恨晚的感覺。
            今天推薦Requests給各位測試人也是有原因的,我們在工作中難免會碰到一些奇葩的性能測試需求,例如測試某個中間件的消息處理效率等,當(dāng)然,如果你熟悉JAVA,他應(yīng)該也是有一個類似的庫的。那么如果你是一個Pythoner,Requests無疑是你的第一選擇,我們來看一下它優(yōu)雅的DEMO:
          >>> r = requests.get('https://api.github.com/user', auth=('user', 'pass'))
          >>> r.status_code
          200
          >>> r.headers['content-type']
          'application/json; charset=utf8'
          >>> r.encoding
          'utf-8'
          >>> r.text
          u'{"type":"User"...'
          >>> r.json()
          {u'private_gists': 419, u'total_private_repos': 77, ...}
            Requests提供了最簡便的JSON解析方法,類似于這樣:
          >>> import requests
          >>> r = requests.get('https://github.com/timeline.json')
          >>> r.json()
          [{u'repository': {u'open_issues': 0, u'url': 'https://github.com/...
            一個自定義header的例子,POST
          >>> import json
          >>> url = 'https://api.github.com/some/endpoint'
          >>> payload = {'some': 'data'}
          >>> headers = {'content-type': 'application/json'}
          >>> r = requests.post(url, data=json.dumps(payload), headers=headers)
            看到這里,各位Pythoner估計已經(jīng)按捺不住激動的心情
            在這里,你可以欣賞到更多API和EG。

          posted on 2014-01-15 10:22 順其自然EVO 閱讀(511) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           
          <2014年1月>
          2930311234
          567891011
          12131415161718
          19202122232425
          2627282930311
          2345678

          導(dǎo)航

          統(tǒng)計

          常用鏈接

          留言簿(55)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 达孜县| 德昌县| 灌阳县| 稷山县| 大庆市| 齐齐哈尔市| 改则县| 图们市| 罗源县| 攀枝花市| 西盟| 新竹市| 房产| 云梦县| 泌阳县| 剑川县| 江油市| 兰坪| 汉沽区| 富源县| 石阡县| 成武县| 大同市| 山东| 于田县| 江安县| 黄石市| 图片| 陆川县| 石林| 布拖县| 乐亭县| 黑水县| 乐东| 遂昌县| 永兴县| 九寨沟县| 孟连| 中卫市| 乌拉特前旗| 宝兴县|