requests接口自动化9-共享session和传递cookie

前言:

   session:用requests.session()创建会话,可以将会话信息传递给其他接口

   cookie:用RequestsCookieJar或者cookie字典传递cookie信息

fildder请求响应内容:

requests接口自动化9-共享session和传递cookie_第1张图片

 

 requests接口自动化9-共享session和传递cookie_第2张图片

 

 代码:

import  requests
from requests.cookies import RequestsCookieJar
#登录
#form表单形式的post请求,用data传参,Content-Type: application/x-www-form-urlencoded
payload={"username":"admin","password":"admin","validateCode":"","rememberMe":"false"}
s=requests.session()
response1=s.post("http://www.luckyframe.cn/login",data=payload)
jsseionid=response1.cookies['JSESSIONID']
#打印的是cookiejar对象
print(response1.cookies)
#打印的是具体cookie值
print(response1.cookies['JSESSIONID'])

#1、不传cookie,直接发送其他请求
response2=requests.get('http://www.luckyframe.cn/testexecution/taskCaseExecute/getMainData.do?_=1592060167330')
#打印响应文本
print(response2.text)

#2、登录后,s里继承了cookie信息,用s可以直接发送其他请求
response3=s.get('http://www.luckyframe.cn/testexecution/taskCaseExecute/getMainData.do?_=1592060167330')
#打印响应文本
print(response3.text)

#3、用cookie传递登录会话信息
cookie={'JSESSIONID':jsseionid}
header={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36'}
response5=requests.get('http://www.luckyframe.cn/testexecution/taskCaseExecute/getMainData.do?_=1592060167330',cookies=cookie,headers=header)
#打印响应文本
print(response5.text)

#4、用RequestsCookieJar传递登录cookie信息
response6=requests.get('http://www.luckyframe.cn/testexecution/taskCaseExecute/getMainData.do?_=1592060167330',cookies=response1.cookies,headers=header)
#打印响应文本
print(response6.text)

#5、用cookie传递登录会话信息
#创建RequestsCookieJar对象
jar=RequestsCookieJar()
#通过set方法添加cookie信息
jar.set('JSESSIONID',jsseionid)

header={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36'}
response7=requests.get('http://www.luckyframe.cn/testexecution/taskCaseExecute/getMainData.do?_=1592060167330',cookies=jar,headers=header)
#打印响应文本
print(response7.text)

#6、用cookie发送post请求
header1={'Content-Type':'application/x-www-form-urlencoded'}
response8=requests.post('http://www.luckyframe.cn/testmanagmt/projectPlan/list',cookies=cookie,headers=header1)
print(response8.text)

 运行结果:

D:\Python37\python.exe E:/PycharmProjects/home_UI/apistudy/testcookie.py
]>
3c734cf5-bee0-4c75-87d0-a6bc85f77ed6



    
    
    
    LuckyFrame开源测试平台
    
    
    
    
    
    
    
    
    
    
    




    

[ LuckyFrame ]

登录:

{"caseadddata":["1","1"],"casedata":["0","0"],"taskdata":["20天0小时34分钟","1"],"logdata":["0","0"]} {"caseadddata":["1","1"],"casedata":["0","0"],"taskdata":["20天0小时34分钟","1"],"logdata":["0","0"]} {"caseadddata":["1","1"],"casedata":["0","0"],"taskdata":["20天0小时34分钟","1"],"logdata":["0","0"]} {"caseadddata":["1","1"],"casedata":["0","0"],"taskdata":["20天0小时34分钟","1"],"logdata":["0","0"]} {"total":1,"rows":[{"searchValue":null,"createBy":"admin","createTime":"2020-06-14 11:32:27","updateBy":"admin","updateTime":"2020-06-14 11:32:27","remark":null,"params":{},"planId":1,"planName":"迭代一","planCaseCount":0,"projectId":2,"project":{"searchValue":null,"createBy":null,"createTime":null,"updateBy":null,"updateTime":null,"remark":null,"params":{},"projectId":2,"projectName":"ICS","deptId":104,"projectSign":"ics","dept":null,"flag":false}}],"code":0} Process finished with exit code 0

  

 

你可能感兴趣的:(requests接口自动化9-共享session和传递cookie)