MessageBody#
- class MessageBody(**kwargs)#
MessageBody represents the request or response body of a
Message.
Note that while length always reflects the full length of the
message body, data is normally None, and will only be filled in
after flatten is called. For client-side
messages, this automatically happens for the response body after it
has been fully read. Likewise, for server-side
messages, the request body is automatically filled in after being
read.
As an added bonus, when data is filled in, it is always terminated
with a \0 byte (which is not reflected in length).
Constructors#
- class MessageBody
- classmethod new() MessageBody#
Creates a new
MessageBody.Messageuses this internally; you will not normally need to call it yourself.
Methods#
- class MessageBody
- append(data: list[int]) None#
Appends
lengthbytes fromdatatobodyaccording touse.- Parameters:
data – data to append
- append_bytes(buffer: Bytes) None#
Appends the data from
buffertobody.- Parameters:
buffer – a
Bytes
- complete() None#
Tags
bodyas being complete.Call this when using chunked encoding after you have appended the last chunk.
- flatten() Bytes#
Fills in
body's data field with a buffer containing all of the data inbody.Adds an additional
\0byte not counted bybody's length field.
- get_accumulate() bool#
Gets the accumulate flag on
body.See [method``MessageBody``.set_accumulate. for details.
- get_chunk(offset: int) Bytes | None#
Gets a
Bytescontaining data frombodystarting atoffset.The size of the returned chunk is unspecified. You can iterate through the entire body by first calling
get_chunkwith an offset of 0, and then on each successive call, increment the offset by the length of the previously-returned chunk.If
offsetis greater than or equal to the total length ofbody, then the return value depends on whether or notcompletehas been called or not; if it has, thenget_chunkwill return a 0-length chunk (indicating the end ofbody). If it has not, thenget_chunkwill returnNone(indicating thatbodymay still potentially have more data, but that data is not currently available).- Parameters:
offset – an offset
- got_chunk(chunk: Bytes) None#
Handles the
MessageBodypart of receiving a chunk of data from the network.Normally this means appending
chunktobody, exactly as withappend_bytes, but if you have setbody's accumulate flag toFalse, then that will not happen.This is a low-level method which you should not normally need to use.
- Parameters:
chunk – a
Bytesreceived from the network
- set_accumulate(accumulate: bool) None#
Sets or clears the accumulate flag on
body.(The default value is
True.) If set toFalse,body's data field will not be filled in after the body is fully sent/received, and the chunks that make upbodymay be discarded when they are no longer needed.If you set the flag to
Falseon theMessagerequest_body of a client-side message, it will block the accumulation of chunks intobody's data field, but it will not normally cause the chunks to be discarded after being written like in the server-sideMessageresponse_body case, because the request body needs to be kept around in case the request needs to be sent a second time due to redirection or authentication.- Parameters:
accumulate – whether or not to accumulate body chunks in
body
- wrote_chunk(chunk: Bytes) None#
Handles the
MessageBodypart of writing a chunk of data to the network.Normally this is a no-op, but if you have set
body's accumulate flag toFalse, then this will causechunkto be discarded to free up memory.This is a low-level method which you should not need to use, and there are further restrictions on its proper use which are not documented here.
- Parameters:
chunk – a
Bytesreturned fromget_chunk