Python接口测试-使用requests模块发送GET请求
本篇主要记录下使用python的requests模块发送GET请求的实现代码.
向服务器发送get请求:
无参数时:r = requests.get(url)
带params时:r = requests.get(url,params=params)
带params和headers时:r = requests.get(url,params=params,headers=headers)
代码如下:
#coding=utf-8
import unittest
import requests class GetTest(unittest.TestCase): def setUp(self):
host = 'https://httpbin.org/'
endpoint = 'get'
self.url = ''.join([host, endpoint]) def test1(self):
u'''get无参数测试'''
r1 = requests.get(self.url)# 向服务器发送请求
code = r1.status_code #状态码
self.assertEqual(200,code)
print(r1.text) # unicode型文本 def test2(self):
u'''get带参数测试'''
params = {'show_env': '1'}
r2 = requests.get(self.url,params=params)
self.assertEqual(200, r2.status_code) def test3(self):
u'''get带参数、带headers测试'''
params = {'show_env': '8'}
headers = {'Connection': 'keep-alive', 'Accept-Encoding': 'gzip, deflate',
'Accept': '*/*','User-Agent': 'python-requests/2.18.3'}
r = requests.get(self.url, params=params,headers=headers)
r3 = r.json()
print(r3)
connect = r3.get('headers').get('Connection')
self.assertEqual('close', connect) #断言 校验header里的Connection值 def tearDown(self):
pass if __name__ == "__main__":
unittest.main()
Python接口测试-使用requests模块发送GET请求的更多相关文章
- Python接口测试-使用requests模块发送post请求
本篇主要记录下使用python的requests模块发送post请求的实现代码. #coding=utf-8 import unittest import requests class PostTes ...
- python - 怎样使用 requests 模块发送http请求
最近在学python自动化,怎样用python发起一个http请求呢? 通过了解 request 模块可以帮助我们发起http请求 步骤: 1.首先import 下 request 模块 2.然后看请 ...
- python接口测试中—Requests模块的使用
Requests模块的使用 中文文档API:http://2.python-requests.org/en/master/ 1.发送get.post请求 import requests reponse ...
- requests模块发送POST请求
在HTTP协议中,post提交的数据必须放在消息主体中,但是协议中并没有规定必须使用什么编码方式,从而导致了 提交方式 的不同.服务端根据请求头中的 Content-Type 字段来获知请求中的消息主 ...
- python 爬虫 基于requests模块的get请求
需求:爬取搜狗首页的页面数据 import requests # 1.指定url url = 'https://www.sogou.com/' # 2.发起get请求:get方法会返回请求成功的响应对 ...
- Python接口测试,Requests模块讲解:GET、POST、Cookies、Session等
文章最下方有对应课程的视频链接哦^_^ 一.安装.GET,公共方法 二.POST 三.Cookies 四.Session 五.认证 六.超时配置.代理.事件钩子 七.错误异常
- 『居善地』接口测试 — 5、使用Requests库发送POST请求
目录 1.请求正文是application/x-www-form-urlencoded 2.请求正文是raw (1)json格式文本(application/json) (2)xml格式文本(text ...
- Python 爬虫二 requests模块
requests模块 Requests模块 get方法请求 整体演示一下: import requests response = requests.get("https://www.baid ...
- python通过get,post方式发送http请求和接收http响应的方法,pythonget
python通过get,post方式发送http请求和接收http响应的方法,pythonget 本文实例讲述了python通过get,post方式发送http请求和接收http响应的方法.分享给大家 ...
随机推荐
- 总结distinct、group by 、row_number()over函数用法及区别
distinct和group by 是一样的,查询去重,只能是全部重复的,也可以理解为针对单例,因为一行有一个字段不一样,他们就会认为这两行内容是不重复的.但是使用row_number()over这个 ...
- 程序员注意【自verycd.com的JavaAmg77 】
展望未来,总结过去10年的程序员生涯,给程序员小弟弟小妹妹们的一些总结性忠告 走过的路,回忆起来是那么曲折,把自己的一些心得体会分享给程序员兄弟姐妹们,虽然时代在变化,但是很可能你也会走我已经做过的1 ...
- border-radius编程练习1-3
border-radius编程练习1-3 我们刚学了圆角的知识,那么我们运用圆角的知识来实现下图所要求的效果: 参考代码: <!DOCTYPE html> <html lang=&q ...
- Markdown 常用语言关键字
Markdown 语法高亮支持的语言还是比较多的,记下来备用. 语言名 关键字 Bash bash CoffeeScript coffeescript C++ cpp C# cs CSS css Di ...
- 希捷powerchoice磁盘休眠功能配置方法
本篇关于希捷磁盘休眠的配置方法 准备设置的软件 下载地址 https://raw.githubusercontent.com/Seagate/ToolBin/master/SeaChest/Power ...
- 彻底卸载MySQL5.7(msi,exe)版
1,停止MySQL服务 2,右键找到任务管理器 3,在程序中卸载MySQL 4,删除MySQL安装目录 有的是在C:\Program Files下,我的是在(X86)下 5,删除隐藏文件中的MySQL ...
- 认识socketserver
1.服务端 # 如果socket起一个tcp服务,在同一个时间只能和一个客户端通信 # 如果socketserver起一个服务,在同一个时间就可以和多个客户端通信了 # socketserver # ...
- MySQL索引背后的数据结构及原理
摘要 本文以MySQL数据库为研究对象,讨论与数据库索引相关的一些话题.特别需要说明的是,MySQL支持诸多存储引擎,而各种存储引擎对索引的支持也各不相同,因此MySQL数据库支持多种索引类型,如BT ...
- Vector和ArrayList的联系和区别
Vector和ArrayList的联系和区别 1.联系: 实用原理相同 功能相同 都是长度可变的数组结构,很多情况下可以互用 2.两者的主要区别:
- 免费|申请谷歌云服务器|msf安装
apt-get install -y wget 参考链接 知乎-免费|申请谷歌云服务器 知乎-免费|申请谷歌云服务器 cnblogs-debian.ubuntu安装metasploit通用方法 谷歌云 ...