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响应的方法.分享给大家 ...
随机推荐
- 在Linux下安装C++的OpenCV 3
最近在看<学习OpenCV3>这本书,所以记录下我在ubuntu16.4下搭建C++版本OpenCV 3.4.5的过程.首先请确保cuda,gcc, g++都安装好了,我这里是cuda 1 ...
- mdp文件-Chapter1-MINIM.mdp
mdp文件是能量最小化,NVT模拟,NPT模拟与MD模拟的必须文件. mdp文件的详细解释可以参考官方文档http://manual.gromacs.org/online/mdp_opt.html 接 ...
- 转 Cache一致性和内存模型
卢本伟牛逼,写得很好 https://wudaijun.com/2019/04/cpu-cache-and-memory-model/ 本文主要谈谈CPU Cache的设计,内存屏障的原理和用法,最后 ...
- 45. 跳跃游戏 II
给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的最大长度. 你的目标是使用最少的跳跃次数到达数组的最后一个位置. 示例: 输入: [2,3,1,1,4]输出 ...
- 《GNU_makefile》第七章——makefile的条件执行
条件执行即,通过变量的值,来控制make的执行和忽略. 条件执行只能控制makefile的make语法部分,不能控制shell部分 1.一个例子 - libs_for_gcc = -lgnu norm ...
- spring的原理
一.pring的原理 1.1 IOC控制反转 ==> 扫描机制通过代理方式动态创建对象 扫描注解,通过反射获取类路径,动态创建对应类的对象,放置在对象池中(多线程做法,防止短时间内创建对象过多, ...
- ceph的ISCSI GATEWAY
前言 最开始接触这个是在L版本的监控平台里面看到的,有个iscsi网关,但是没看到有类似的介绍,然后通过接口查询到了一些资料,当时由于有比较多的东西需要新内核,新版本的支持,所以并没有配置出来,由于内 ...
- Spring扩展之二:ApplicationListener
1.介绍 用于监听应用程序事件的接口. 子接口:GenericApplicationListener,SmartApplicationListener. 通过ApplicationEvent类和App ...
- Javaweb项目页面实时显示后台处理结果
http://www.cnblogs.com/dong-xu/p/6701271.html 此博文甚好,项目参照博主代码可实现. 前端页面: <%@ page language="ja ...
- Metasploit 脚本Web传递(Web Delivery)
Metasploit 脚本Web传递(Web Delivery)