python接口自动化测试(四)-Cookie&Sessinon

简介:   掌握了前面几节的的内容,就可以做一些简单的http协议接口的请求发送了,但是这些还不够。HTTP协议是一个无状态的应用层协议,也就是说前后两次请求是没有任何关系的,那如果我们测试的接口之前有相互依赖关系怎么办呢(比如我要在博客园发文章,是需要先登录的),这时我们就要用到cookie和sessio...

  掌握了前面几节的的内容,就可以做一些简单的http协议接口的请求发送了,但是这些还不够。HTTP协议是一个无状态的应用层协议,也就是说前后两次请求是没有任何关系的,那如果我们测试的接口之前有相互依赖关系怎么办呢(比如我要在博客园发文章,是需要先登录的),这时我们就要用到cookie和session技术来保持客户端与服务器端连接的状态,这也就是本节要介绍的内容:

 

一、Cookie:

1、获取cookie:

# -*- coding:utf-8 -*-
#获取cookie
import requests
import json

url = "https://www.baidu.com/"
r = requests.get(url)

#将RequestsCookieJar转换成字典
c = requests.utils.dict_from_cookiejar(r.cookies)

print r.cookies
print c

for a in r.cookies:
    print a.name,a.value

输出:

<RequestsCookieJar[<Cookie BDORZ=27315 for .baidu.com/>]>
{'BDORZ': '27315'}
BDORZ 27315

 

二、发送cookie:

# -*- coding:utf-8 -*-
#发送cookie到服务器
import requests
import json

host = "http://httpbin.org/"
endpoint = "cookies"

url
= ''.join([host,endpoint]) #方法一:简单发送 # cookies = {"aaa":"bbb"} # r = requests.get(url,cookies=cookies) # print r.text #方法二:复杂发送 s = requests.session() c = requests.cookies.RequestsCookieJar() c.set('c-name','c-value',path='/xxx/uuu',domain='.test.com') s.cookies.update(c)

 

 

二、Session

1、保持会话同步:

# -*- coding:utf-8 -*-
import requests
import json

host = "http://httpbin.org/"
endpoint = "cookies"

url = ''.join([host,endpoint])
url1 = "http://httpbin.org/cookies/set/sessioncookie/123456789"

r = requests.get(url)
print r.text
print "------"

s = requests.session() #初始化一个session对象 s.get(url1) #cookie的信息存在了session中 r = s.get(url) print r.text

输出:

{
  "cookies": {}
}

------
{
  "cookies": {
    "sessioncookie": "123456789"
  }
}

 

2、保存会话信息:

# -*- coding:utf-8 -*-
import requests
import json

host = "http://httpbin.org/"
endpoint = "headers"

url = ''.join([host,endpoint])

header1 = {"testA":"AAA"}
header2 = {"testB":"BBB"}

s = requests.session()    #初始化一个session对象
s.headers.update(header1)   #已经存在于服务中的信息
r = s.get(url,headers=header2) #发送新的信息

print r.text

输出:

{
  "headers": {
    "Accept": "*/*", 
    "Accept-Encoding": "gzip, deflate", 
    "Connection": "close", 
    "Host": "httpbin.org", 
    "Testa": "AAA", 
    "Testb": "BBB", 
    "User-Agent": "python-requests/2.18.1"
  }
}

 

3、删除已存在的会话信息,保存为None

# -*- coding:utf-8 -*-
import requests
import json

host = "http://httpbin.org/"
endpoint = "headers"

url = ''.join([host,endpoint])

header1 = {"testA":"AAA"}
header2 = {"testB":"BBB"}

s = requests.session()    #初始化一个session对象
s.headers.update(header1)   #已经存在于服务中的信息
r = s.get(url,headers=header2) #发送新的信息

print r.text

print '--------'

s.headers['testA'] = None   #删除会话里的信息testA
r1 = s.get(url,headers = header2)
print r1.text
{
  "headers": {
    "Accept": "*/*", 
    "Accept-Encoding": "gzip, deflate", 
    "Connection": "close", 
    "Host": "httpbin.org", 
    "Testa": "AAA", 
    "Testb": "BBB", 
    "User-Agent": "python-requests/2.18.1"
  }
}

--------
{
  "headers": {
    "Accept": "*/*", 
    "Accept-Encoding": "gzip, deflate", 
    "Connection": "close", 
    "Host": "httpbin.org", 
    "Testb": "BBB", 
    "User-Agent": "python-requests/2.18.1"
  }
}

 

4、提供默认数据:

s = requests.Session()
s.auth = ('user', 'pass')
s.headers.update({'x-test': 'true'})

# both 'x-test' and 'x-test2' are sent
s.get('http://httpbin.org/headers', headers={'x-test2': 'true'})

 

 

 

参考:

http://docs.python-requests.org/en/master/user/quickstart/#cookies

http://docs.python-requests.org/en/master/user/advanced/#session-objects

目录
相关文章
|
Python
python中3种获取cookie解决方案
python中3种获取cookie解决方案
217 0
|
4月前
|
存储 网络协议 测试技术
【如何学习Python自动化测试】—— Cookie 处理
【如何学习Python自动化测试】—— Cookie 处理
36 1
|
4月前
|
编解码 测试技术 Python
【如何学习Python自动化测试】—— 浏览器操作
【如何学习Python自动化测试】—— 浏览器操作
25 0
|
4月前
|
存储 搜索推荐 安全
Python中的Cookie模块有什么功能
Python中的Cookie模块有什么功能
53 0
|
4月前
|
IDE 测试技术 开发工具
Python 的安全性和测试:在 Python 中如何进行单元测试?
Python 的安全性和测试:在 Python 中如何进行单元测试?
81 2
|
Python
Python request如何做接口测试
Python request如何做接口测试
|
JSON 数据安全/隐私保护 数据格式
python fastapi 入门教程,每个案例都使用postman进行测试写的接口
python fastapi 入门教程,每个案例都使用postman进行测试写的接口
|
存储 JSON 测试技术
Unittest+Python接口自动化测试如何进行token关联?
Unittest+Python接口自动化测试如何进行token关联?
150 0
|
数据采集 测试技术 数据安全/隐私保护
python使用pytest接口自动化测试的使用
python使用pytest接口自动化测试的使用
367 0
 python使用pytest接口自动化测试的使用
|
JSON JavaScript 测试技术
通过Postman工具如何快速批量生成Python Pytest的测试脚本
github上找到了一个别人开源的脚本,clone下来试了一下,仓库地址如下:该仓库的脚本可以将postman导出的json格式的collections的文件转换为一个指定模板的.py的python脚本。并且提供了cli的命令行去进行转换,也提供了转换的demo,也可以将这个作为一个library安装到自己的python安装目录下的site-packages中去。
通过Postman工具如何快速批量生成Python Pytest的测试脚本