本篇主要记录下使用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请求的更多相关文章

  1. Python接口测试-使用requests模块发送post请求

    本篇主要记录下使用python的requests模块发送post请求的实现代码. #coding=utf-8 import unittest import requests class PostTes ...

  2. python - 怎样使用 requests 模块发送http请求

    最近在学python自动化,怎样用python发起一个http请求呢? 通过了解 request 模块可以帮助我们发起http请求 步骤: 1.首先import 下 request 模块 2.然后看请 ...

  3. python接口测试中—Requests模块的使用

    Requests模块的使用 中文文档API:http://2.python-requests.org/en/master/ 1.发送get.post请求 import requests reponse ...

  4. requests模块发送POST请求

    在HTTP协议中,post提交的数据必须放在消息主体中,但是协议中并没有规定必须使用什么编码方式,从而导致了 提交方式 的不同.服务端根据请求头中的 Content-Type 字段来获知请求中的消息主 ...

  5. python 爬虫 基于requests模块的get请求

    需求:爬取搜狗首页的页面数据 import requests # 1.指定url url = 'https://www.sogou.com/' # 2.发起get请求:get方法会返回请求成功的响应对 ...

  6. Python接口测试,Requests模块讲解:GET、POST、Cookies、Session等

    文章最下方有对应课程的视频链接哦^_^ 一.安装.GET,公共方法 二.POST 三.Cookies 四.Session 五.认证 六.超时配置.代理.事件钩子 七.错误异常

  7. 『居善地』接口测试 — 5、使用Requests库发送POST请求

    目录 1.请求正文是application/x-www-form-urlencoded 2.请求正文是raw (1)json格式文本(application/json) (2)xml格式文本(text ...

  8. Python 爬虫二 requests模块

    requests模块 Requests模块 get方法请求 整体演示一下: import requests response = requests.get("https://www.baid ...

  9. python通过get,post方式发送http请求和接收http响应的方法,pythonget

    python通过get,post方式发送http请求和接收http响应的方法,pythonget 本文实例讲述了python通过get,post方式发送http请求和接收http响应的方法.分享给大家 ...

随机推荐

  1. 将unicode 转为字符串

    public string chn(string source)// convert contents to GB2312 { return new Regex(@"\\u([0-9A-F] ...

  2. NIO源码分析:SelectionKey

    SelectionKey SelectionKey,选择键,在每次通道注册到选择器上时都会创建一个SelectionKey储存在该选择器上,该SelectionKey保存了注册的通道.注册的选择器.通 ...

  3. Cocos Creator与VS Code整合代码提示问题

    Cocos Creator与VS Code整合开发配置 在Cocos Creator中依次点击下面框中的菜单 VS Code工作流 配置Cocos Creator的默认编辑器 Cocos Creato ...

  4. 变强——GitHub 热点速览 Vol.46

    作者:HelloGitHub-小鱼干 网络不通怎么办?Ping 就是你的调试大招,而 gping 则是 Ping 的内功,终端可视化显示 Ping 结果,一眼看明数据接收情况.前端调试大招又是什么呢? ...

  5. 实现一个简易vue

    vue主要的功能实现主要分为3部分: 数据劫持/数据代理:数据改变时通知相关函数进行更新操作 数据依赖收集:建立保存dom节点与数据的关联关系 模板与数据之间的绑定:接受到新数据时对dom节点进行更新 ...

  6. Innodb之(临时)表空间、段、区、块

    引用连接:https://www.cnblogs.com/duanxz/p/3724120.html 对于innodb存储引擎而言,其数据文件最小的存储单位为页.默认为16KB大小.在页的基础上又分为 ...

  7. [原题复现]百度杯CTF比赛 十月场 WEB EXEC(PHP弱类型)

    简介  原题复现:  考察知识点:PHP弱类型.  线上平台:https://www.ichunqiu.com/battalion(i春秋 CTF平台) 过程 看源码发现这个 vim泄露  下方都试了 ...

  8. 记php多张图片 合并生成竖列 纵向长图(可用于商品详情图合并下载)

    <?php namespace app\mapi\common\image; /** * 拼接多幅图片成为一张图片 * * 参数说明:原图片为文件路径数组,目的图片如果留空,则不保存结果 * * ...

  9. configure.ac和Makefile.am的格式解析概述

    1. configure.ac和Makefile.am的格式解析概述 1.1. Autotools相关工具链 1.1.1. Autotools 1.1.2. 其他相关工具 1.2. 工具链的流程 1. ...

  10. Django rest framework 基础

    01: Django rest framework 基础 ​ ​ 1.1 什么是RESTful 1. REST与技术无关,代表的是一种软件架构风格(REST是Representational Stat ...