问题:
为了测试,写的sever,下面仅为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[]>