python 接口自动化测试二(request.get)
环境搭建好后,接下来我们先来了解一下requests的一些简单使用,主要包括:
- requests常用请求方法使用,包括:get,post
- requests库中的Session、Cookie的使用
- 其它高级部分:认证、代理、证书验证、超时配置、错误异常处理等。
本节首先来了解一下requests库中如何发送get请求:
一、看下方法定义:
1、到官方文档去了下requests.get()方法的定义,如下:

2、点击右上角的【source】,看一下它的源码如下:

看到最后一行return,get方法最后是通过调用 requests.request 方法实现的,其实在其它的请求方法如post,put,head,delete等方法都是调用的request方法,然后把请求方法的类型传递给request方法第一个参数。
3、HTTP协议是一个基于请求/响应模式的、无状态的,应用层协议。既然有请求,就有响应,来看下resquest中常用的响应信息:

二、get方法简单使用:
1、不带参数的get:

# -*- coding:utf-8 -*-
#不带参数的get import requests
import json host = "http://httpbin.org/"
endpoint = "get" url = ''.join([host,endpoint])
r = requests.get(url)
#response = r.json() print type(r.text)
print (eval(r.text))

输出:

{
'origin': '183.14.133.88',
'headers': {
'Connection': 'close',
'Host': 'httpbin.org',
'Accept-Encoding': 'gzip,
deflate',
'Accept': '*/*',
'User-Agent': 'python-requests/2.18.1'
},
'args': {
},
'url': 'http: //httpbin.org/get'
}


# -*- coding:utf-8 -*-
#带参数的get import requests
import json host = "http://httpbin.org/"
endpoint = "get" url = ''.join([host,endpoint])
params = {"show_env":"1"}
r = requests.get(url=url,params=params) print r.url

输出:

http://httpbin.org/get?show_env=1
{
'origin': '183.14.133.88',
'headers': {
'X-Request-Id': 'ebe922b4-c463-4fe9-9faf-49748d682fd7',
'Accept-Encoding': 'gzip,
deflate',
'X-Forwarded-Port': '80',
'Total-Route-Time': '0',
'Connection': 'close',
'Connect-Time': '0',
'Via': '1.1vegur',
'X-Forwarded-For': '183.14.133.88',
'Accept': '*/*',
'User-Agent': 'python-requests/2.18.1',
'X-Request-Start': '1504755961007',
'Host': 'httpbin.org',
'X-Forwarded-Proto': 'http'
},
'args': {
'show_env': '1'
},
'url': 'http: //httpbin.org/get?show_env=1'
}

3、带header的get:

# -*- coding:utf-8 -*- import requests
import json host = "http://httpbin.org/"
endpoint = "get" url = ''.join([host,endpoint])
headers = {"User-Agent":"test request headers"} r = requests.get(url)
r = requests.get(url,headers=headers)
#response = r.json()
print (eval(r.text))['headers']['User-Agent']

输出:
test request headers
4、同时带参数和header:

# -*- coding:utf-8 -*-
import requests
import json host = "http://httpbin.org/"
endpoint = "get" url = ''.join([host,endpoint])
headers = {"User-Agent":"test request headers"}
params = {"show_env":"1"} r = requests.get(url)
r = requests.get(url,headers=headers,params=params) #response = r.json()
print (eval(r.text))['headers']['User-Agent']
print r.url

输出:
test request headers
http://httpbin.org/get?show_env=1
python 接口自动化测试二(request.get)的更多相关文章
- python接口自动化测试二十七:密码MD5加密 ''' MD5加密 ''' # 由于MD5模块在python3中被移除 # 在python3中使用hashlib模块进行md5操作 import hashlib # 待加密信息 str = 'asdas89799,.//plrmf' # 创建md5对象 hl = hashlib.md5() # Tips # 此处必须声明encode # 若写法为
python接口自动化测试二十七:密码MD5加密 ''' MD5加密 '''# 由于MD5模块在python3中被移除# 在python3中使用hashlib模块进行md5操作import has ...
- python接口自动化测试(二)-requests.get()
环境搭建好后,接下来我们先来了解一下requests的一些简单使用,主要包括: requests常用请求方法使用,包括:get,post requests库中的Session.Cookie的使用 其它 ...
- python接口自动化测试(一)-request模块
urllib.request模块是python3针对处理url的. 1. 首先导入: from urllib import request 2. 构造url,构造url的headers信息和传参[re ...
- python接口自动化测试二:常用操作
url = '接口地址' r = requests.get(url) # 发送get请求 print(r.status_code) ...
- python接口自动化测试二十三:文件上传
# 以禅道为例: 一.创建一个类,类里面写一个登录方法: import requestsclass LoginZentao(): def __init__(self, s): # 初始化 self.s ...
- python接口自动化测试二十六:使用pymysql模块链接数据库
#!/usr/bin/env python# -*- coding: utf-8 -*-# @Time : 2018/5/28 18:51# @Author : StalloneYang# ...
- python接口自动化测试二十五:执行所有用例,并生成HTML测试报告
import requestsimport unittest class TestQQ(unittest.TestCase): '''测试QQ号接口''' # 此注释将展示到测 ...
- python接口自动化测试二十九:yaml配置文件的写和读
# 先安装ruamel.yaml模块 写入配置文件: import os# 先安装ruamel.yaml模块from ruamel import yaml # 将字典写入到yamldict = { ' ...
- python接口自动化测试二十八:连接SQL sever操作
1.中文乱码问题: (1).文件头加上# -*- coding:utf-8 -*- 或者 #coding=utf8 (2).pymssql.connect连接串中charset是要跟你数据库的编码一样 ...
随机推荐
- spring @Transactional 事务注解
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.SERIALIZABLE, rollbackFor = ...
- 保存一个经常用的Makefile
############################################################# # Generic Makefile for C/C++ Program # ...
- python接口自动化测试(八)-unittest-生成测试报告
用例的管理问题解决了后,接下来要考虑的就是报告我问题了,这里生成测试报告主要用到 HTMLTestRunner.py 这个模块,下面简单介绍一下如何使用: 一.下载HTMLTestRunner下载: ...
- react diff 原理
(1) 把树形结构按照层级分解,只比较同级元素.(2) 列表结构的每个单元添加唯一的 key 属性,方便比较.(3) React 只会匹配相同 class 的 component(这里面的 class ...
- 浅谈 CSS 预处理器: 为什么要使用预处理器?
CSS 自诞生以来,基本语法和核心机制一直没有本质上的变化,它的发展几乎全是表现力层面上的提升.最开始 CSS 在网页中的作用只是辅助性的装饰,轻便易学是最大的需求:然而如今网站的复杂度已经不可同日而 ...
- CountDownLatch、CyclicBarrier和Semaphore 使用示例及原理
备注:博客园的markDown格式支持的特别不友好.也欢迎查看我的csdn的此篇文章链接:CountDownLatch.CyclicBarrier和Semaphore 使用示例及原理 CountDow ...
- 关于go语言的测试相关内容笔记
其实之前对于测试自己一直比较弱,不管是python的还是go的,关于测试这块并没有非常注重,这次就好好整理一下关于go的测试 单元测试 Go程序主要包含三类测试: 功能测试(test).基准测试(be ...
- HTML5学习笔记(二十九):Cookie和Session
HTTP协议本身是无状态的,这和HTTP最初的设计是相符的,每次请求都是创建一个短连接,发送请求,得到数据后就关闭连接.即每次连接都是独立的一次连接. 这样的话,导致的问题就是当我在一个页面登陆了账号 ...
- C#通过用户名与密码访问共享目录
C#通过用户名与密码访问共享目录 using System; using System.Collections.Generic; using System.Linq; using System.Tex ...
- [Linux 性能调优] 网卡中断与CPU的绑定问题
在Linux的网络调优方面,如果你发现网络流量上不去,那么有一个方面需要去查一下:网卡处理网络请求的中断是否被绑定到单个CPU(或者说跟处理其它中断的是同一个CPU). 先说一下背景 网卡与操作系统的 ...