Request和Response同时继承Message对象

Message对象属性

Message对象属性 作用 例子
flow.request/flow.response.headers 获取协议头 直接headers即可
flow.request/flow.response.http_version 获取HTTP协议版本 直接使用
http_version
flow.request/flow.response.content 未压缩的HTTP消息主体
(以bytes为单位)
直接content即可
flow.request/flow.response.raw_content 可能被压缩过的HTTP消息主体
(以bytes为单位)
直接使用
raw_content
flow.request/flow.response.text 返回text 直接使用text即可
flow.request/flow.response.timestamp_start 获取request第一个字节的
时间戳
直接使用timestamp_start
flow.request/flow.response.timestamp_end 获取request最后一个字节的
时间戳
直接使用
timestamp_end
flow.request/flow.response.trailers 获取request的trailers
Trailer规定哪个标头
用作分块消息后的元信息
直接trailers即可

Message对象方法

Message对象方法 作用 例子
flow.request/flow.response.decode(self, strict=True) 根据当前request的
Content-Encoding标头进行解码
当Content-Encoding标头不存在
并且strict=True时
即触发ValueError异常
直接decode()即可
flow.request/flow.response.encode(self, e) 对当前request进行编码
编码格式有:
“gzip”, “deflate”, “identity”, “br”
直接使用encode('gzip')
flow.request/flow.response.get_state(self) 获取request的状态 直接使用get_state()
flow.request/flow.response.set_state(self, state) 设置request的状态 直接使用set_state(state)
要和get_state()
配合使用
flow.request/flow.response.get_content(self, strict: bool = True) -> bytes 使用内容编码标头
(例如gzip)解码的
request消息正文
直接使用
get_content()
返回二进制数据
flow.request/flow.response.set_content(self, value: Union[bytes, NoneType]) -> None 设置request的
二进制数据
直接使用set_content(b'xxx')
可用get_content()查看新内容
flow.request/flow.response.get_text(self, strict: bool = True) -> Union[str, NoneType] 使用内容编码标头
(例如gzip)和Content-Encoding标头字符集
解码的HTTP消息正文
此方法可以自动解码
源码有多种方案解码
直接使用get_text()
返回str
flow.request/flow.response.set_text(self, text: Union[str, NoneType]) -> None 设置request的text 直接使用set_text('xxx')
可用get_text()
查看新内容