python WEB接口自动化测试之requests库详解
更新时间:2021-10-25 03:44:30 作者:admin 浏览:493
python WEB接口自动化测试之requests库详解
1.GET请求
1.1查看get函数的使用
>>> help(requests.get) #查看requests库的属性get请求函数的使用
Help on function get in module requests.api:
get(url, params=None, **kwargs)
Sends a GET request.
:param url: URL for the new :class:`Request` object.
:param params: (optional) Dictionary or bytes to be sent in the query string for the :class:`Request`.
:param **kwargs: Optional arguments that ``request`` takes.
:return: :class:`Response ` object
:rtype: requests.Response
1.2 requests的get函数的入参说明
url:调用接口的URL地址。
params:为可选参数,该参数是一个字典类型。数据会以键/值对的形式置于 URL 中,跟在一个问号的后面。举例说明,若r=requests.get("http://httpbin.org/get",params={'key1':'value1','key2':'value2'}) 那么,最终请求的目标URL为http://bin.org/get?key1=val1& key2=val2。
**kwargs:其他可选参数,例如headers,files,cookies,auth,timeout,json等。
例如:auth=(‘username,’password’):接口安全测试中用到的用户认证。
例如:headers = {'user-agent': 'my-app/0.0.1'}:可定制发送请求头。
1.3 requests函数的返回值(http响应)
返回的是response类对象(requests.models.Response)。来自requests模块 models.py里的Response类
>>> r=requests.get('http://httpbin.org/get') #查看response的属性
>>> dir(r)
['__attrs__', '__bool__', '__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__getstate__', '__hash__', '__init__', '__iter__', '__module__', '__new__', '__nonzero__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setstate__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_content', '_content_consumed', 'apparent_encoding', 'close', 'connection', 'content', 'cookies', 'elapsed', 'encoding', 'headers', 'history', 'is_permanent_redirect', 'is_redirect', 'iter_content', 'iter_lines', 'json', 'links', 'ok', 'raise_for_status', 'raw', 'reason', 'request', 'status_code', 'text','url']
对于返回对象常用的属性如下:
json():生成json数据对象的方法。如果 JSON 解码失败, r.json 就会抛出一个异常。例如,响应内容是 401 (Unauthorized),尝试访问 r.json 将会抛出 ValueError: No JSON object could be decoded 异常。
status_code:响应状态码。
encoding:编码,如utf-8。
url:目标url。
headers:响应头。类型为字典类型,若键不存在则返回None。
text:响应内容。字符串方式,会自动根据响应头部的字符编码进行解码。如果你改变了编码r.encoding,每当你访问 r.text ,Request 都将会使用 r.encoding 的新值。
content:二进制响应内容。字节方式,会自动为你解码gzip和deflate压缩。
raw:原始响应内容,也就是 urllib 的 response 对象,请求中要加stream=True,再使用 r.raw.read() 读取。
r.raise_for_status:失败请求(非200响应)抛出异常。
1.4举例说明
>>> payload={'key1':'value1','key2':'value2'}
>>> r=requests.get("http://httpbin.org/get",params=payload)
>>> r.status_code
200
>>> r.json()
{u'origin': u'113.98.252.236', u'headers': {u'Host': u'httpbin.org', u'Accept-Encoding': u'gzip, deflate', u'Accept': u'*/*', u'User-Agent': u'python-requests/2.7.0 CPython/2.7.11 Windows/7'}, u'args': {u'key2': u'value2', u'key1': u'value1'}, u'url': u'http://httpbin.org/get?key2=value2&key1=value1'}
>>> r.url #url:params数据会以键/值对的形式置于 URL 中,跟在一个问号的后面。
u'http://httpbin.org/get?key2=value2&key1=value1'
>>> r.headers
{'content-length': '334', 'server': 'nginx', 'connection': 'keep-alive', 'access-control-allow-credentials': 'true', 'date': 'Fri, 09 Dec 2016 09:04:40 GMT', 'access-control-allow-origin': '*', 'content-type': 'application/json'}
>>> r.headers['content-type']
'application/json'
>>> r.raw
>>> r.cookies
>>> r.text
u'{ "args": { "key1": "value1", "key2": "value2" }, "headers": { "Accept": "*/*", "Accept-Encoding": "gzip, deflate", "Host": "httpbin.org", "User-Agent": "python-requests/2.7.0 CPython/2.7.11 Windows/7" }, "origin": "113.98.252.236", "url": "http://httpbin.org/get?key2=value2&key1=value1" } '
>>> r.content
'{ "args": { "key1": "value1", "key2": "value2" }, "headers": { "Accept": "*/*", "Accept-Encoding": "gzip, deflate", "Host": "httpbin.org", "User-Agent": "python-requests/2.7.0 CPython/2.7.11 Windows/7" }, "origin": "113.98.252.236", "url": "http://httpbin.org/get?key2=value2&key1=value1" } '
>>> payload = {'key1': 'value1', 'key2': 'value2'}
>>> r = requests.get("http://httpbin.org/get", params=payload,stream=True)
>>> r.raw
>>> r.raw.read()
'{ "args": { "key1": "value1", "key2": "value2" }, "headers": { "Accept": "*/*", "Accept-Encoding": "gzip, deflate", "Host": "httpbin.org", "User-Agent": "python-requests/2.7.0 CPython/2.7.11 Windows/7" }, "origin": "113.98.252.236", "url": "http://httpbin.org/get?key2=value2&key1=value1" } '
1.5用fiddler查看抓包情况
r.json():json数据,可以看出与1.4中的r.json()值一致。
r.headers:响应头数据,可以看出与1.4中r.headers值一致。
r.raw:响应原始数据
1.6 get请求总结
综上所述,通过requests.get("某url",params={字典类型参数键值对})模拟浏览器发送一个http的请求(其中请求的方法是get,请求的url地址如下形式
http://httpbin.org/get?key2=value2&key1=value1),服务器处理数据后,会返回一个response对象,通过读取response对象的属性值,如json数据,可以做一系列的断言,从而验证该接口返回的数据是否正确。
2.POST请求
2.1查看post函数的使用
>>> help(requests.post) #查看requests库的属性post请求函数的使用
post(url, data=None, json=None, **kwargs)
Sends a POST request.
:param url: URL for the new :class:`Request` object.
:param data: (optional) Dictionary, bytes, or file-like object to send in the body of the :class:`Request`.
:param json: (optional) json data to send in the body of the :class:`Request`.
:param **kwargs: Optional arguments that ``request`` takes.
:return: :class:`Response ` object
:rtype: requests.Response
2.2 requests的post函数的入参说明
url:调用接口的URL地址。
data:为可选参数,该参数是一个字典类型。
json:为可选参数,该参数是一个json类型。
**kwargs:其他可选参数,例如headers等。
2.3 requests函数的返回值(http响应)
同1.3
json():生成json数据对象的方法。如果 JSON 解码失败, r.json 就会抛出一个异常。例如,响应内容是 401 (Unauthorized),尝试访问 r.json 将会抛出 ValueError: No JSON object could be decoded 异常。
status_code:响应状态码。
encoding:编码,如utf-8。
url:目标url。
headers:响应头。类型为字典类型,若键不存在则返回None。
text:响应内容。字符串方式,会自动根据响应头部的字符编码进行解码。如果你改变了编码r.encoding,每当你访问 r.text ,Request 都将会使用 r.encoding 的新值。
content:二进制响应内容。字节方式,会自动为你解码gzip和deflate压缩。
raw:原始响应内容,也就是 urllib 的 response 对象,请求中要加stream=True,再使用 r.raw.read() 读取。
r.raise_for_status:失败请求(非200响应)抛出异常。
2.4举例说明
>>> payload = {'key1': 'value1', 'key2': 'value2'}
>>> r = requests.post("http://httpbin.org/post", data=payload)
>>> r.status_code
200
>>> r.json()
{u'files': {}, u'origin': u'113.98.252.236', u'form': {u'key2': u'value2', u'key1': u'value1'}, u'url': u'http://httpbin.org/post', u'args': {}, u'headers': {u'Content-Length': u'23', u'Accept-Encoding': u'gzip,deflate', u'Accept': u'*/*', u'User-Agent': u'python-requests/2.7.0 CPython/2.7.11 Windows/7', u'Host': u'httpbin.org', u'Content-Type': u'application/x-www-form-urlencoded'}, u'json': None, u'data': u''}
>>> r.url
u'http://httpbin.org/post'
>>> r.headers
{'content-length': '461', 'server': 'nginx', 'connection': 'keep-alive', 'access-control-allow-credentials': 'true', 'date': 'Mon, 12 Dec 2016 02:46:14 GMT', 'access-control-allow-origin': '*', 'content-type': 'application/json'}
>>> r.headers['content-type']
'application/json'
>>> r.raw
>>> r.text
u'{ "args": {}, "data": "", "files": {}, "form": { "key1": "value1", "key2": "value2" }, "headers": { "Accept": "*/*", "Accept-Encoding": "gzip, deflate", "Content-Length": "23", "Content-Type": "application/x-www-form-urlencoded", "Host": "httpbin.org", "User-Agent": "python-requests/2.7.0 CPython/2.7.11 Windows/7" }, "json": null, "origin": "113.98.252.236", "url": "http://httpbin.org/post" } '
>>> r.content
'{ "args": {}, "data": "", "files": {}, "form": { "key1": "value1", "key2": "value2" }, "headers": { "Accept": "*/*", "Accept-Encoding": "gzip, deflate", "Content-Length": "23", "Content-Type": "application/x-www-form-urlencoded", "Host": "httpbin.org", "User-Agent": "python-requests/2.7.0 CPython/2.7.11 Windows/7" }, "json": null, "origin": "113.98.252.236", "url": "http://httpbin.org/post" } '
>>> payload = {'key1': 'value1', 'key2': 'value2'}
#获取原始响应内容
>>> r = requests.post("http://httpbin.org/post", data=payload,stream=True)
>>> r.raw
>>> r.raw.read()
'{ "args": {}, "data": "", "files": {}, "form": { "key1": "value1", "key2": "value2" }, "headers": { "Accept": "*/*", "Accept-Encoding": "gzip, deflate", "Content-Length": "23", "Content-Type": "application/x-www-form-urlencoded", "Host": "httpbin.org", "User-Agent": "python-requests/2.7.0 CPython/2.7.11 Windows/7" }, "json": null, "origin": "113.98.252.236", "url": "http://httpbin.org/post" } '
#使用 json 参数直接传递
>>> payload = {'key1': 'value1', 'key2': 'value2'}
>>> r = requests.post("http://httpbin.org/post", json=payload)
>>> r.text
u'{ "args": {}, "data": "{"key2": "value2", "key1": "value1"}", "files": {}, "form": {}, "headers": { "Accept": "*/*", "Accept-Encoding": "gzip, deflate", "Content-Length": "36", "Content-Type": "application/json", "Host": "httpbin.org", "User-Agent": "python-requests/2.7.0 CPython/2.7.11 Windows/7" }, "json": { "key1": "value1", "key2": "value2" }, "origin": "113.98.252.236", "url": "http://httpbin.org/post" } '
2.5用fiddler查看抓包情况
r.json():json数据,可以看出与2.4中的r.json()值一致。
注:通过json进行传参的json数据
r.headers:响应头数据,可以看出与2.4中r.headers值一致。
r.raw:响应原始数据
2.6 post请求总结
综上所述,通过requests.post("某url",data={字典类型参数键值对})模拟浏览器发送一个http的请求(其中请求的方法是post,请求的url地址如下形式
http://httpbin.org/get),服务器处理数据后,会返回一个response对象,通过读取response对象的属性值,如json数据,可以做一系列的断言,从而验证该接口返回的数据是否正确。
3.其他用法
3.1 定制请求头
>>> payload = {'key1': 'value1', 'key2': 'value2'}
>>> headers = {'user-agent': 'my-app/0.0.1'}
>>> r = requests.post("http://httpbin.org/post", data=payload,headers=headers)
用fiddler抓包,可以看到,发送请求的请求头中的user-agent的值为设置的值。
注意: 所有的 header 值必须是 string、bytestring 或者 unicode。
3.2上传文件
>>> import os
>>> os.getcwd()
'D:pythontest'
>>> f=open('1.txt','w+')
>>> f.write('test')
>>> os.listdir('D:pythontest')
['1.txt',]
>>> import requests
>>> url = 'http://httpbin.org/post'
>>> files = {'file': open('1.txt', 'rb')}
>>> r = requests.post(url, files=files)
>>> r.text
u'{ "args": {}, "data": "", "files": { "file": "" }, "form": {}, "headers": { "Accept": "*/*", "Accept-Encoding": "gzip, deflate", "Content-Length": "141", "Content-Type": "multipart/form-data; boundary=37de3eb22a754f34849771891b77bd23", "Host": "httpbin.org", "User-Agent": "python-requests/2.7.0 CPython/2.7.11 Windows/7" }, "json": null, "origin": "113.98.252.236", "url": "http://httpbin.org/post" } '
用fiddler抓包,可以看到,响应的JSON数据中有file。
3.3 cookies的发送
>>> url = 'http://httpbin.org/cookies'
>>> cookies = dict(cookies_are='working')
>>> r = requests.get(url, cookies=cookies)
>>> r.text
u'{ "cookies": { "cookies_are": "working" } } '
以上是关于python WEB接口自动化测试之requests库的详解,由多测师亲自撰写。https://www.aichudan.com/