使用Python的Requests库进行web接口测试

发表于:2017-7-18 11:08

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:sunshine_zhf    来源:博客

  1、Requests简介
  Requests 是使用 Apache2 Licensed 许可证的 HTTP 库。用 Python 编写,真正的为人类着想。
  Python 标准库中的 urllib2 模块提供了你所需要的大多数 HTTP 功能,但是它的 API 太渣了。它是为另一个时代、另一个互联网所创建的。它需要巨量的工作,甚至包括各种方法覆盖,来完成最简单的任务。
  总之,大家以后对urllib2库敬而远之就行了。来拥抱Requests吧。
  Requests的官方文档:http://cn.python-requests.org/zh_CN/latest/
  通过下面方法安装requests:  
pip install requests
  2、Requests如何发送HTTP请求
  非常简单,先导入requests,
import requests 
  然后,按照下面的方法发送http的各种请求:
  r = requests.get('https://github.com/timeline.json')  
  r = requests.post("http://httpbin.org/post")  
  r = requests.put("http://httpbin.org/put")  
  r = requests.delete("http://httpbin.org/delete")  
  r = requests.head("http://httpbin.org/get")  
  r = requests.options("http://httpbin.org/get")  
  3、为URL传递参数
  如果http请求需要带URL参数(注意是URL参数不是body参数),那么需要将参数附带到payload字典里头,按照下面的方法发送请求:
  import requests  
  payload = {'key1': 'value1', 'key2': 'value2'}  
  r = requests.get("http://httpbin.org/get",params=payload)  
  print r.url 
   通过print(r.url)能看到URL已被正确编码:
http://httpbin.org/get?key2=value2&key1=value1  
  注意字典里值为 None 的键都不会被添加到 URL 的查询字符串里。
  4、unicode响应内容
  import requests
  r = requests.get('https://github.com/timeline.json')  
  r.text  
  响应结果是:
  {"message":"Hello there, wayfaring stranger. If you're reading this then you probably didn't see our blog post a couple of years back announcing that this API would Go away: http://Git.io/17AROg Fear not, you should be able to get what you need from the shiny new Events API instead.","documentation_url":"https://developer.github.com/v3/activity/events/#list-public-events"}
  Requests会自动解码来自服务器的内容。大多数unicode字符集都能被无缝地解码。请求发出后,Requests会基于HTTP头部对响应的编码作出有根据的推测。当你访问r.text之时,Requests会使用其推测的文本编码。你可以找出Requests使用了什么编码,并且能够使用r.encoding 属性来改变它
   >>> r.encoding
  'utf-8'
  5、二进制响应内容
  如果请求返回的是二进制的图片,你可以使用r.content访问请求响应体。
  import requests  
  from PIL import Image  
  from StringIO import StringIO  
  r = requests.get('http://cn.python-requests.org/zh_CN/latest/_static/requests-sidebar.png')  
  i = Image.open(StringIO(r.content))  
  i.show() 
  6、JSON响应内容
  Requests中也有一个内置的JSON解码器,助你处理JSON数据
  import requests  
  r = requests.get('https://github.com/timeline.json')  
  print r.json() 
  r.json将返回的json格式字符串解码成python字典。r.text返回的utf-8的文本。
  7、定制请求头
  如果你想为请求添加HTTP头部,只要简单地传递一个 dict 给headers 参数就可以了。
  import requests  
  import  json  
  payload = {'some': 'data'}  
  headers = {'content-type': 'application/json'}  
  r = requests.get('https://github.com/timeline.json', data=json.dumps(payload), headers=headers)  
  print r.json() 
  注意,这里的payload是放到body里面的,所以params参数要使用json数据。
  8、POST请求
  就像上面‘定制请求头’中的例子,将payload序列化为json格式数据,传递给data参数。
  9、POST提交文件
  先制作一个text文件,名为‘report.txt’,内容是‘this is a file’。Requests使得上传多部分编码文件变得很简单:
  import requests  
  url = 'http://httpbin.org/post'  
  files = {'file': open('report.txt', 'rb')}  
  r = requests.post(url, files=files)  
  print r.text 
  返回结果是:
  C:\Python27\python.exe C:/Users/Administrator/PycharmProjects/flaskexample/postfile.py  
  {  
  "args": {},   
  "data": "",   
  "files": {  
      <strong>"file": "this is a file"</strong>  
    },   
  "form": {},   
  "headers": {  
  "Accept": "*/*",   
  "Accept-Encoding": "gzip, deflate",   
  "Content-Length": "160",   
  "Content-Type": "multipart/form-data; boundary=a3b41a6300214ffdb55ddbc23dfc0d91",   
  "Host": "httpbin.org",   
  "User-Agent": "python-requests/2.7.0 CPython/2.7.9 Windows/2012Server"  
    },   
  "json": null,   
  "origin": "202.108.92.226",   
  "url": "http://httpbin.org/post"  
  }  
  Process finished with exit code 0  

31/3123>
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号