最近自己和伙伴写的项目涉及前后端完全分离多个客户端,自己测试登录接口想利用请求获取Cookie中的sessionid作为状态通信和身份认证的信息。请求获取cookie打印的函数找了半天。于是就记下来,给需要的同学和自己以后。
核心代码:
import requests from http import cookiejar formData = { 'username':162210702234, 'password':'bigsai', } headers = { 'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:57.0) Gecko/20100101 Firefox/57.0', 'cookie':'' } url="http://localhost:8080/login" req=requests.post(url,data=formData,headers=headers) cookies = requests.utils.dict_from_cookiejar(req.cookies) print(cookies) for key in cookies.keys(): print(key,cookies.get(key)) print(req.text)
结果