使用python发送得到请求实现测试工具

  介绍

今天就跟大家聊聊有关使用python发送得到请求实现测试工具,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

<强>背景

这一节我们实现简单的命令行发送得到请求的工具,使用方式如下:

 python。py www.v2ex.com/api/nodes/show.json \, # 63;名字\=python
  接口地址:http://www.v2ex.com/api/nodes/show.json& # 63; name=python
  
  状态码: 200
  
  标题:
  时间:星期二,2018年7月10 07:06:12格林尼治时间
  内容类型:application/json; charset=utf - 8
  传输编码:分块
  连接:维生
  不同:接受编码
  X-Rate-Limit-Remaining: 119
  到期:星期二,2018年7月10 08:03:49格林尼治时间
  服务器:星系/3.9.8.1
  Etag: W/?6 a33d25372411dc6fa4190a5cf9679caa0edc2a"
  X-Rate-Limit-Reset: 1531209600
  cache - control:信息=3600
  X-Rate-Limit-Limit: 120
  谷歌:XY
  内容编码:gzip
  Strict-Transport-Security:信息=31536000
  {
  “id":90,
  “name":“python"
  “url":“https://www.v2ex.com/go/python"
  “title":“Python"
  “title_alternative":“Python"
  “topics":9530,
  “stars":6601,
  
  “header":“这里讨论各种Python语言编程话题,也包括Django,龙卷风等框架的讨论。这里是一个能够帮助你解决实际问题的地方!”,
  
  
  “footer":零,
  
  “created":1278683336,
  “avatar_mini":“//cdn.v2ex.com/navatar/8613/985e/90_mini.png& # 63; m=1531131631“,
  “avatar_normal":“//cdn.v2ex.com/navatar/8613/985e/90_normal.png& # 63; m=1531131631“,
  “avatar_large":“//cdn.v2ex.com/navatar/8613/985e/90_large.png& # 63; m=1531131631,
  }

主要使用场景是快速访问http api的接口,查看状态码,响应头以及响应内容。

<强>代码实现

简单起见,我们会用库到请求。

进口要求
  从系统导入argv
  
  使用=& # 39;& # 39;& # 39;
  用法:
  python。py https://api.github.com
  & # 39;& # 39;& # 39;
  
  如果len (argv) !=2:
  打印(使用)
  退出()
  
  script_name、url=argv
  
  如果url [4] !=& # 39; http # 39;:
  url=& # 39; http://& # 39;+网址
  
  r=requests.get (url)
  
  打印(f"接口地址:{url} \ n")
  打印(f"状态码:{r.status_code} \ n")
  打印(f"头:“)
  关键,价值r.headers.items ():
  print (f"{关键}:{value}“)
  
  打印(r.text) 

看完上述内容,你们对使用python发送得到请求实现测试工具有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注行业资讯频道,感谢大家的支持。

使用python发送得到请求实现测试工具