qileilove

blog已经转移至github,大家请访问 http://qaseven.github.io/

Python优雅的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不应该是这样,当我们试图让http库更加优雅的时候,我们找到了Requests,有一种相见恨晚的感觉。
  今天推荐Requests给各位测试人也是有原因的,我们在工作中难免会碰到一些奇葩的性能测试需求,例如测试某个中间件的消息处理效率等,当然,如果你熟悉JAVA,他应该也是有一个类似的库的。那么如果你是一个Pythoner,Requests无疑是你的第一选择,我们来看一下它优雅的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估计已经按捺不住激动的心情
  在这里,你可以欣赏到更多API和EG。

posted on 2014-01-15 10:22 顺其自然EVO 阅读(493) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航:
 
<2014年1月>
2930311234
567891011
12131415161718
19202122232425
2627282930311
2345678

导航

统计

常用链接

留言簿(55)

随笔分类

随笔档案

文章分类

文章档案

搜索

最新评论

阅读排行榜

评论排行榜