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是要跟你数据库的编码一样 ...
随机推荐
- jqeury-地区三级联动
html+js <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www ...
- Python 汉字转拼音
本文参考: Python中文转拼音代码(支持全拼和首字母缩写) 中文中不可以有“()” # -*- coding: utf-8 -*- __version__ = '0.9' __all__ = [& ...
- Spark2.3(四十):如何使用java通过yarn api调度spark app,并根据appId监控任务,关闭任务,获取任务日志
背景: 调研过OOZIE和AZKABA,这种都是只是使用spark-submit.sh来提交任务,任务提交上去之后获取不到ApplicationId,更无法跟踪spark application的任务 ...
- fopen和fopen_s用法的比较
open和fopen_s用法的比较 fopen 和 fopen_s fopen用法: fp = fopen(filename,"w"). fop ...
- jvm理论-class文件
当JVM运行Java程序的时候,它会加载对应的class文件,并提取class文件中的信息存放在JVM的方法区内存中. Class文件组成 1.Class文件是一组以8位字节为基础单位的二进制流,各个 ...
- Android 获取Activity当前view
View cv = getWindow().getDecorView(); 来自为知笔记(Wiz)
- Python-MacOSX下SIP引起的pip权限问题解决方案(非取消SIP机制)
网上很多资料都是取消SIP机制,安装完再恢复.可是基于用户的权限来安装模块包显得更加合理. 第一种:(推荐)pip install module --user -U http://www.jiansh ...
- 评分模型的检验方法和标准&信用评分及实现
评分模型的检验方法和标准通常有:K-S指标.交换曲线.AR值.Gini数等.例如,K-S指标是用来衡量验证结果是否优于期望值,具体标准为:如果K-S大于40%,模型具有较好的预测功能,发展的模型具有成 ...
- 时间序列分解算法:STL
1. 详解 STL (Seasonal-Trend decomposition procedure based on Loess) [1] 为时序分解中一种常见的算法,基于LOESS将某时刻的数据\( ...
- 在SQL Server 2017 中,当Alwasyon group启用了DTC_SUPPORT = PER_DB, 会导致无法创建replicaiton.
当Alwasyon group启用了DTC_SUPPORT = PER_DB, 会导致无法创建replicaiton.无法修改已经存在的replication. 原因: 当当Alwasyon grou ...