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

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

  掌握了前面几节的的内容,就可以做一些简单的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

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值