遇一山,过一山,处处有风景;只要勇敢向前,一路尽是繁花盛开。 | (点击查看→)【测试干货】python/java自动化、持续集成、性能、测开、简历、笔试面试等

记录一次群答问:requests获取cookie

问题: 

为了测试,写的server,下面仅为set cookie的部分代码 

response = make_response('{"code":9420, "msg":"恭喜%s,登录成功","token":"%s"}'%(username, token))
            response.set_cookie(username,token)  # 设置cookie
            return response

 

测试 

import requests

data = {"username":"qzcsbj","password":"123456"}
url="http://127.0.0.1:9999/login"
r = requests.post(url,data)
print(r)
print(r.status_code)
print(r.text)
print(r.cookies)  # response.cookies返回的是RequestsCookieJar对象
print(requests.utils.dict_from_cookiejar(r.cookies))
print(requests.utils.dict_from_cookiejar(r.cookies)['qzcsbj'])

 

输出结果: 

<Response [200]>
200
{"code":9420, "msg":"恭喜qzcsbj,登录成功","token":"970aa0ddb521bcde8ce7f1592a889e1c"}
<RequestsCookieJar[<Cookie qzcsbj=970aa0ddb521bcde8ce7f1592a889e1c for 127.0.0.1/>]>
{'qzcsbj': '970aa0ddb521bcde8ce7f1592a889e1c'}
970aa0ddb521bcde8ce7f1592a889e1c

 

如果没有上面set cookie的代码,

print(r.cookies)的输出结果就是<RequestsCookieJar[]>

 

posted @ 2019-08-13 06:28  全栈测试笔记  阅读(2253)  评论(1编辑  收藏  举报
浏览器标题切换
浏览器标题切换end