当前位置: 首页 > news >正文

代做通一样的网站网站开发asp.net

代做通一样的网站,网站开发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参数。 最后感谢每一个认真阅读我文章的人礼尚往来总是要有的这些资料对于【软件测试】的朋友来说应该是最全面最完整的备战仓库虽然不是什么很值钱的东西如果你用得到的话可以直接拿走 这些资料对于【软件测试】的朋友来说应该是最全面最完整的备战仓库这个仓库也陪伴上万个测试工程师们走过最艰难的路程希望也能帮助到你
http://www.zqtcl.cn/news/352349/

相关文章:

  • 律师手机网站模板天津做推广的公司
  • 西安市高新区建设规划局网站织梦小说网站模板下载地址
  • 网站开发简历 自我评价网页设计报告论文
  • 如何让网站不被收录不备案 国内网站
  • 站长之家域名买天猫店铺去哪里买
  • asp.net做的网站模板下载万网x3 wordpress
  • 设计网站设计目标天津市建设工程管理总队网站
  • 网站开始怎么做上海响应式网页建设
  • 网站备案 seo免费二维码制作网站
  • 删除网站备案网站建设湖南岚鸿建设
  • 做vlogger的网站有哪些长沙网站排名技巧
  • 媒体营销平台商品seo关键词优化
  • 芜湖先锋网站两学一做wordpress菜单顶部
  • 网站策划怎么样一级域名网站如何申请
  • 烟台高端网站开发网站开发哪个公司好
  • 广州网站定制开发方案南宁网站 制作
  • php做网站需要后台吗郑州建网站十大
  • 网站跳出率是什么意思百度服务
  • 建站 discuz开发者导航
  • 有哪些网站可以做毕业设计外贸网站发外链
  • 如何使用网站模板计算机培训班有用吗
  • 本地宁波网站建设电子商务网站建设工具都有那些
  • 网站建设的基本目标免费 wordpress企业主题
  • 专业网站建设微信商城开发规划馆网站建设
  • 网站建设公司沈阳西安建设工程信息交易中心官网
  • 青海住房和城乡建设部网站wordpress php7.3
  • 网站后台重置密码怎么做360网站怎么做网址链接
  • 广告网站建设及推广网站建设怎样推广
  • 做网站使网页不居中滁州注册公司流程和费用
  • 做网站广告经营者个性定制网站