本篇主要记录下使用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. 使用RD Client来远程桌面

    使用RD Client来远程桌面 可能你会觉得奇怪,team viewer和向日葵之类的难道不香吗?看起来他们两个都是实现了远程桌面的功能,好像没必要特地用Windows自带的RD Client进行内 ...

  2. 3 jinja2模板

    video17 jinja2过滤器 过滤器通过管道符号进行使用.如{{ name | length }}将返回name的长度,过滤器相当于是一个函数. 1 def hello_world(): 2 i ...

  3. 2Git分支问题

    1,查看所有分支: git branch *号在哪表明当前分支在哪. 2,新建一个分支: git branch featureq(分支名) 转到该分支下: git checkout featureq ...

  4. lua调用dll demo

    使用的是lua5.3 DllMain.cpp 1 //生成的dll 是 lua_add53.dll 2 //luaopen_lua_add 3 extern "C" { 4 #in ...

  5. TextView之富文本

    项目中使用富文本比较常见了,一行显示多种样式颜色的文本,使用 ClickableSpan 富文本实现在同一个 TextView 中的文本的颜色.大小.背景色等属性的多样化和个性化. 我们也可以使用Ht ...

  6. 02、Hibernate开发步骤

    1.创建Hibernate配置文件(hibernate.cfg.xml) <?xml version="1.0" encoding="UTF-8"?> ...

  7. ASP.NET Core使用HostingStartup增强启动操作

    概念 在ASP.NET Core中我们可以使用一种机制来增强启动时的操作,它就是HostingStartup.如何叫"增强"操作,相信了解过AOP概念的同学应该都非常的熟悉.我们常 ...

  8. impala语句

    0.保留两位小数 round(字段a, 需要保留几位小数) round( data, 4) 1. case wen case when 字段a = '01' and 字段b = '01' and 字段 ...

  9. 标准库之collections

    collections 模块----Python标准库,是数据结构常用模块 常用类型有: 计数器(Counter)   dict的子类,计算可hash的对象: 双端队列(deque)  类似于list ...

  10. 插件SimSynth合成器功能介绍

    本章节采用图文结合的方式给大家介绍下电音编曲软件"水果"FL Studio中SimSynth合成器的功能介绍,感兴趣的朋友可以一起进来沟通交流哦. SimSynth插件是FL St ...