顺通建设集团有限公司 网站,wordpress评论头像问题,请问有没有做网站,免费域名网站哪个最好场景#xff1a;如何获取登录时响应消息中的sessionid#xff0c;以及如何在后续请求中把sessionid添到cookie中
Requests库提供了一个Session类#xff0c;通过requests库中的session对象#xff0c;requests库会自动帮我们保存服务端返回的cookie数据(set-cookie里的内容…场景如何获取登录时响应消息中的sessionid以及如何在后续请求中把sessionid添到cookie中
Requests库提供了一个Session类通过requests库中的session对象requests库会自动帮我们保存服务端返回的cookie数据(set-cookie里的内容)也会在HTTP发出请求时自动在消息头中放入cookie数据。
用py模拟客户端接收响应消息
import requests# 打印HTTP响应消息的函数
def printResponse(response):print(\n\n-------- HTTP response * begin -------)print(response.status_code) #打印响应状态码for k, v in response.headers.items():print(f{k}: {v})print()print(response.content.decode(utf8)) # 获取响应消息体并转换成字符串print(-------- HTTP response * end -------\n\n)# 创建 Session 对象
s requests.Session()# 通过 Session 对象发送http请求但是是添加资源信息 ; urlencoded格式构建请求消息体
response s.post(http://127.0.0.1/api/mgr/signin,data{username: byhy,password: 88888888})printResponse(response) # 通过 Session 对象发送http请求但是是从服务端获取资源 (模拟列出所有客户) ; 构建请求之URL参数
response s.get(http://127.0.0.1/api/mgr/customers,params{action : list_customer,pagesize : 10,pagenum : 1,keywords : ,})printResponse(response) # 注意这里是请求消息发出去了打印的是响应消息 1.python中的items()方法 response得到的一般是json类型的字符串response.headers.items()在字典对象后面加上.items()就会得到一个包含字典中所有键值对的视图对象然后用循环遍历返回的这个视图对象。 参考python中字典的items函数_python中items()函数-CSDN博客 2.python中的print(f): 格式化字符串语法允许在字符串中嵌入表达式在f中的{}内可以放入任何有效的py表达式在print函数执行时表达式会被求值并放入到字符串的相应位置。 参考自python中的print(f‘‘)具体用法_python print f-CSDN博客 本文参考自 requests库 和 session - 白月黑羽 (byhy.net)