代做通一样的网站,网站开发asp.net,网站建设有几种工具,2022腾讯云网站建设方案书由于web接口自动化测试需要用到python的第三方库--requests库#xff0c;运用requests库可以模拟发送http请求#xff0c;再结合unittest测试框架#xff0c;就能完成web接口自动化测试。 所以笔者今天先来总结一下requests库的用法。希望对大家#xff08;尤其是新手… 由于web接口自动化测试需要用到python的第三方库--requests库运用requests库可以模拟发送http请求再结合unittest测试框架就能完成web接口自动化测试。 所以笔者今天先来总结一下requests库的用法。希望对大家尤其是新手有帮助哦大家可要仔细阅读加油
1.GET请求 1.1查看get函数的使用 1.2 requests的get函数的入参说明 1.3 requests函数的返回值http响应 1.4举例说明 1.5用fiddler查看抓包情况 1.6 get请求总结
2.POST请求 2.1查看post函数的使用 2.2 requests的post函数的入参说明 2.3 requests函数的返回值http响应 2.4举例说明 2.5用fiddler查看抓包情况 2.6 post请求总结
3.其他用法 3.1 定制请求头 3.2上传文件 3.3 cookies的发送
4.知识拓展 4.1关于GET和POST的区别 4.2关于请求的Headers的Accept-Encoding说明 4.3关于响应的Headers的Content-Type说明 4.4关于requests资料相关地址
前提
requests库是python的第三方库需要提前安装哦可以直接用pip命令python –m pip install requests 按照惯例先将requests库的属性打印出来看看哪些属性。 import requests dir(requests) #查看requests库的属性
[ConnectionError, HTTPError, NullHandler, PreparedRequest, Request, RequestException, Response, Session, Timeout, TooManyRedirects, URLRequired, __author__, __build__, __builtins__, __copyright__, __doc__, __file__, __license__, __name__, __package__, __path__, __title__, __version__, adapters, api, auth, certs, codes, compat, cookies, delete, exceptions, get, head, hooks, logging, models, options, packages, patch, post, put, request, session, sessions, status_codes, structures, utils]
所以可以看到requests的属性有getpostdeleteput对应http请求的method方法。
常用的是get和post请求。get请求一般是查询获取资源信息。post一般是更新资源信息。 1.GET请求
点击返回目录
1.1查看get函数的使用 help(requests.get) #查看requests库的属性get请求函数的使用
Help on function get in module requests.api:
get(url, paramsNone, **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 Response object :rtype: requests.Response 1.2 requests的get函数的入参说明
url调用接口的URL地址。
params:为可选参数该参数是一个字典类型。数据会以键/值对的形式置于 URL 中跟在一个问号的后面。举例说明若rrequests.get(http://httpbin.org/get,params{key1:value1,key2:value2}) 那么最终请求的目标URL为http://bin.org/get?key1val1 key2val2。
**kwargs:其他可选参数例如headersfilescookiesauthtimeoutjson等。
例如auth(‘username,’password’)接口安全测试中用到的用户认证。
例如headers {user-agent: my-app/0.0.1}可定制发送请求头。 1.3 requests函数的返回值http响应
返回的是response类对象requests.models.Response。来自requests模块 models.py里的Response类 rrequests.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 对象请求中要加streamTrue再使用 r.raw.read() 读取。
r.raise_for_status:失败请求非200响应抛出异常。 1.4举例说明 payload{key1:value1,key2:value2} rrequests.get(http://httpbin.org/get,paramspayload) r.status_code 200 r.json()
{uorigin: u113.98.252.236, uheaders: {uHost: uhttpbin.org, uAccept-Encoding: ugzip, deflate, uAccept: u*/*, uUser-Agent: upython-requests/2.7.0 CPython/2.7.11 Windows/7}, uargs: {ukey2: uvalue2, ukey1: uvalue1}, uurl: uhttp://httpbin.org/get?key2value2key1value1} r.url #urlparams数据会以键/值对的形式置于 URL 中跟在一个问号的后面。 uhttp://httpbin.org/get?key2value2key1value1 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 requests.packages.urllib3.response.HTTPResponse object at 0x0000000002E64E10 r.cookies RequestsCookieJar[] r.text
u{\n args: {\n key1: value1, \n key2: value2\n }, \n headers: {\n Accept: */*, \n Accept-Encoding: gzip, deflate, \n Host: httpbin.org, \n User-Agent: python-requests/2.7.0 CPython/2.7.11 Windows/7\n }, \n origin: 113.98.252.236, \n url: http://httpbin.org/get?key2value2key1value1\n}\n r.content
{\n args: {\n key1: value1, \n key2: value2\n }, \n headers: {\n Accept: */*, \n Accept-Encoding: gzip, deflate, \n Host: httpbin.org, \n User-Agent: python-requests/2.7.0 CPython/2.7.11 Windows/7\n }, \n origin: 113.98.252.236, \n url: http://httpbin.org/get?key2value2key1value1\n}\n payload {key1: value1, key2: value2} r requests.get(http://httpbin.org/get, paramspayload,streamTrue) r.raw
requests.packages.urllib3.response.HTTPResponse object at 0x0000000003105940 r.raw.read()
{\n args: {\n key1: value1, \n key2: value2\n }, \n headers: {\n Accept: */*, \n Accept-Encoding: gzip, deflate, \n Host: httpbin.org, \n User-Agent: python-requests/2.7.0 CPython/2.7.11 Windows/7\n }, \n origin: 113.98.252.236, \n url: http://httpbin.org/get?key2value2key1value1\n}\n 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/getkey2value2key1value1服务器处理数据后会返回一个response对象通过读取response对象的属性值如json数据可以做一系列的断言从而验证该接口返回的数据是否正确。 2.POST请求
点击返回目录
2.1查看post函数的使用 help(requests.post) #查看requests库的属性post请求函数的使用
post(url, dataNone, jsonNone, **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 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 对象请求中要加streamTrue再使用 r.raw.read() 读取。
r.raise_for_status:失败请求非200响应抛出异常。 2.4举例说明 payload {key1: value1, key2: value2} r requests.post(http://httpbin.org/post, datapayload) r.status_code
200 r.json()
{ufiles: {}, uorigin: u113.98.252.236, uform: {ukey2: uvalue2, ukey1: uvalue1}, uurl: uhttp://httpbin.org/post, uargs: {}, uheaders: {uContent-Length: u23, uAccept-Encoding: ugzip,deflate, uAccept: u*/*, uUser-Agent: upython-requests/2.7.0 CPython/2.7.11 Windows/7, uHost: uhttpbin.org, uContent-Type: uapplication/x-www-form-urlencoded}, ujson: None, udata: u} r.url
uhttp://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
requests.packages.urllib3.response.HTTPResponse object at 0x0000000002EE4A58 r.text
u{\n args: {}, \n data: , \n files: {}, \n form: {\n key1: value1, \n key2: value2\n }, \n headers: {\n Accept: */*, \n Accept-Encoding: gzip, deflate, \n Content-Length: 23, \n Content-Type: application/x-www-form-urlencoded, \n Host: httpbin.org, \n User-Agent: python-requests/2.7.0 CPython/2.7.11 Windows/7\n }, \n json: null, \n origin: 113.98.252.236, \n url: http://httpbin.org/post\n}\n r.content
{\n args: {}, \n data: , \n files: {}, \n form: {\n key1: value1, \n key2: value2\n }, \n headers: {\n Accept: */*, \n Accept-Encoding: gzip, deflate, \n Content-Length: 23, \n Content-Type: application/x-www-form-urlencoded, \n Host: httpbin.org, \n User-Agent: python-requests/2.7.0 CPython/2.7.11 Windows/7\n }, \n json: null, \n origin: 113.98.252.236, \n url: http://httpbin.org/post\n}\n payload {key1: value1, key2: value2}
#获取原始响应内容 r requests.post(http://httpbin.org/post, datapayload,streamTrue) r.raw
requests.packages.urllib3.response.HTTPResponse object at 0x0000000003105208 r.raw.read()
{\n args: {}, \n data: , \n files: {}, \n form: {\n key1: value1, \n key2: value2\n }, \n headers: {\n Accept: */*, \n Accept-Encoding: gzip, deflate, \n Content-Length: 23, \n Content-Type: application/x-www-form-urlencoded, \n Host: httpbin.org, \n User-Agent: python-requests/2.7.0 CPython/2.7.11 Windows/7\n }, \n json: null, \n origin: 113.98.252.236, \n url: http://httpbin.org/post\n}\n
#使用 json 参数直接传递 payload {key1: value1, key2: value2} r requests.post(http://httpbin.org/post, jsonpayload) r.text
u{\n args: {}, \n data: {\\key2\\: \\value2\\, \\key1\\: \\value1\\}, \n files: {}, \n form: {}, \n headers: {\n Accept: */*, \n Accept-Encoding: gzip, deflate, \n Content-Length: 36, \n Content-Type: application/json, \n Host: httpbin.org, \n User-Agent: python-requests/2.7.0 CPython/2.7.11 Windows/7\n }, \n json: {\n key1: value1, \n key2: value2\n }, \n origin: 113.98.252.236, \n url: http://httpbin.org/post\n}\n 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, datapayload,headersheaders) 用fiddler抓包可以看到发送请求的请求头中的user-agent的值为设置的值。
注意: 所有的 header 值必须是 string、bytestring 或者 unicode。 3.2上传文件 import os os.getcwd()
D:\\pythontest fopen(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, filesfiles) r.text
u{\n args: {}, \n data: , \n files: {\n file: \n }, \n form: {}, \n headers: {\n Accept: */*, \n Accept-Encoding: gzip, deflate, \n Content-Length: 141, \n Content-Type: multipart/form-data; boundary37de3eb22a754f34849771891b77bd23, \n Host: httpbin.org, \n User-Agent: python-requests/2.7.0 CPython/2.7.11 Windows/7\n }, \n json: null, \n origin: 113.98.252.236, \n url: http://httpbin.org/post\n}\n 用fiddler抓包可以看到响应的JSON数据中有file。 3.3 cookies的发送 url http://httpbin.org/cookies cookies dict(cookies_areworking) r requests.get(url, cookiescookies) r.text u{\n cookies: {\n cookies_are: working\n }\n}\n 4.知识拓展
点击返回目录
4.1关于GET和POST的区别 通过上面抓包可以看出 GET请求的数据会附在URL之后就是 把数据放置在HTTP协议头中以?分割URL和传输数据参数之间以相连如login.action?namehyddd passwordidontknowverify%E4%BD%A0%E5%A5%BD。如果数据是英文字母/数字原样发送如果是空格转换为如果是中文/其他字符则直接把字符串用BASE64加密得出如%E4%BD%A0%E5%A5%BD其中XX中的XX为该符号以 16进制表示的ASCII。 POST把提交的数据则放置在是HTTP包的包体中。 4.2关于请求的Headers的Accept-Encoding说明
请求的headers中的client项中的Accept-Encoding的例如Accept-Encoding: gzip, deflate。浏览器申明自己接收的编码方法通常指定压缩方法是否支持压缩支持什么压缩方法gzipdeflate注意这不是只字符编码。 4.3关于响应的Headers的Content-Type说明
Content-Type是返回消息中非常重要的内容表示后面的文档属于什么MIME类型。
Content-Type: [type]/[subtype]; parameter。例如最常见的就是text/html它的意思是说返回的内容是文本类型这个文本又是HTML格式的。原则上浏览器会根据 Content-Type来决定如何显示返回的消息体内容。
type有下面的形式
Text用于标准化地表示的文本信息文本消息可以是多种字符集和或者多种格式的
Multipart用于连接消息体的多个部分构成一个消息这些部分可以是不同类型的数据
Application用于传输应用程序数据或者二进制数据
Message用于包装一个E-mail消息
Image用于传输静态图片数据
Audio用于传输音频或者音声数据
Video用于传输动态影像数据可以是与音频编辑在一起的视频数据格式。
subtype用于指定type的详细形式。
parameter可以用来指定附加的信息更多情况下是用于指定text/plain和text/htm等的文字编码方式的charset参数。
最后感谢每一个认真阅读我文章的人礼尚往来总是要有的这些资料对于【软件测试】的朋友来说应该是最全面最完整的备战仓库虽然不是什么很值钱的东西如果你用得到的话可以直接拿走 这些资料对于【软件测试】的朋友来说应该是最全面最完整的备战仓库这个仓库也陪伴上万个测试工程师们走过最艰难的路程希望也能帮助到你