本系列教程我们将使用python实现一些简单的测试工具,为了尽可能的简单,我们的工具以命令行工具为主。
本系列教程使用的python版本是3.6.3。
背景
这一节我们实现简单的命令行发送get请求的工具,使用方式如下:
python get.py www.v2ex.com/api/nodes/show.json\"76a33d25372411dc6fa4190a5cf9679caa0edc2a" X-Rate-Limit-Reset : 1531209600 Cache-Control : max-age=3600 X-Rate-Limit-Limit : 120 Google : XY Content-Encoding : gzip Strict-Transport-Security : max-age=31536000 { "id" : 90, "name" : "python", "url" : "https://www.v2ex.com/go/python", "title" : "Python", "title_alternative" : "Python", "topics" : 9530, "stars" : 6601, "header" : "这里讨论各种 Python 语言编程话题,也包括 Django,Tornado 等框架的讨论。这里是一个能够帮助你解决实际问题的地方。", "footer" : null, "created" : 1278683336, "avatar_mini" : "//cdn.v2ex.com/navatar/8613/985e/90_mini.png", "avatar_normal" : "//cdn.v2ex.com/navatar/8613/985e/90_normal.png", "avatar_large" : "//cdn.v2ex.com/navatar/8613/985e/90_large.png" }
主要使用场景是快速访问http的api接口,查看状态码,响应头以及响应内容。
代码实现
简单起见,我们会用到requests库。
import requests from sys import argv USAGE = ''' USAGE: python get.py https://api.github.com ''' if len(argv) != 2: print(USAGE) exit() script_name, url = argv if url[:4] != 'http': url = 'http://' + url r = requests.get(url) print(f"接口地址: {url}\n") print(f"状态码: {r.status_code}\n") print(f"Headers:") for key, value in r.headers.items(): print(f"{key} : {value}") print(r.text)
动手时间
- 抄一遍代码,看自己能不能运行起来
- 给这段代码每一行都加上注释,理解代码做了些什么
- 如果需要在发送get请求的时候默认加上Content-Type: application/json的headers,这段代码该如何修改
源码地址
https://github.com/easonhan007/simple_test_tools
以上就是python实现测试工具(一)——命令行发送get请求的详细内容,更多关于python 命令行发送get请求的资料请关注其它相关文章!
风云阁资源网 Design By www.bgabc.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
风云阁资源网 Design By www.bgabc.com
暂无评论...
更新日志
2024年10月02日
2024年10月02日
- 纪钧瀚《钢琴阅读时光 雨中书店聆听轻音乐》[FLAC/分轨][399.62MB]
- 证声音乐图书馆《走向自然 疗心爵士乐》[320K/MP3][87.4MB]
- 证声音乐图书馆《走向自然 疗心爵士乐》[FLAC/分轨][184.94MB]
- 陈慧娴.2018-Priscilla-Ism演唱会3CD(2024环球红馆40复刻系列)【环球】【WAV+CUE】
- 郑秀文.1999-我应该得到(国)【华纳】【WAV+CUE】
- 陈家慧.2011-钢琴酒吧2CD【龙吟唱片】【WAV+CUE】
- 证声音乐图书馆《雨季 蓝调吉他 Rainy Blues》[320K/MP3][45.01MB]
- 证声音乐图书馆《雨季 蓝调吉他 Rainy Blues》[FLAC/分轨][109.13MB]
- 赞多《序章》[320K/MP3][45.54MB]
- 许巍.2004-每一刻都是崭新的【步升大风】【WAV+CUE】
- 群星.2024-四方馆影视原声带【韶愔音乐】【FLAC分轨】
- 陈雷.1997-安锁咧【金圆唱片】【WAV+CUE】
- 关淑怡.2013-MY.FAVORITE.SK.3CD【环球】【WAV+CUE】
- Sweety.2006-花言乔语【丰华】【WAV+CUE】
- 李恕权.2003-回·20年全精选2CD【SONY】【WAV+CUE】