本篇主要记录下使用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. 关于ThreadLocal的那些事

    这篇文章(看完了再看下面的)很好地解释了ThreadLocal是什么,怎么用,是否能解决线程安全问题,非常nice.但个人认为讲解存在遗漏,进行如下示例补充: InheritableThreadLoc ...

  2. tcp 接收被动关闭 fin

    void tcp_rcv_established(struct sock *sk, struct sk_buff *skb, const struct tcphdr *th, unsigned int ...

  3. dp:322. Coin Change 自下而上的dp

    You are given coins of different denominations and a total amount of money amount. Write a function ...

  4. Linux_Python版本控制

    第1步:更新gcc,因为gcc版本太老会导致新版本python包编译不成功 复制代码代码如下: yum -y install gcc 系统会自动下载并安装或更新,等它自己结束 第2步:安装wget,这 ...

  5. C#高级编程之特性

    特性定义 MSDN的描述:使用特性,可以有效地将元数据或声明性信息与代码(程序集.类型.方法.属性等)相关联. 将特性与程序实体相关联后,可以在运行时使用反射这项技术查询特性. 参考此处作者的解释 h ...

  6. webpack 无法打包:No configuration file found and no output filename configured via CLI option

    报错内容 No configuration file found and no output filename configured via CLI option.A configuration fi ...

  7. PHP弱类型漏洞学习

    简介 PHP在使用双等号(==)判断的时候,不会严格检验传入的变量类型,同时在执行过程中可以将变量自由地进行转换类型.由于弱数据类型的特点,在使用双等号和一些函数时,会造成一定的安全隐患 eg: &l ...

  8. java后端开发三年!你还不了解Spring 依赖注入,凭什么给你涨薪

    前言 前两天和一个同学吃饭的时候同学跟我说了一件事,说他公司有个做了两年的人向他提出要涨薪资,他就顺口问了一个问题关于spring依赖注入的,那个要求涨薪的同学居然被问懵了...事后回家想了想这一块确 ...

  9. 如何用EasyRecovery恢复受损的SD卡?

    SD卡的主要功能是拓展便携式设备.包括:数据相机.手机及其他的多媒体播放器等的存储空间,缓解设备本身的存储压力.即便是在产品内存已经逐渐增加的情况下,还是拥有一大批的忠实用户. 很多用户反应,SD卡使 ...

  10. 使用FL Studio来制作停顿的效果

    停顿效果是一种在音乐创作中非常常用的音效,它能起到缓冲的作用,而且能使这段旋律更具节奏感,在比较激情的歌曲中尤为常见.例如知名歌手王力宏演唱的<火力全开>中就使用了停顿效果,为歌曲加了不少 ...