Pyro4.message — Pyro wire protocol message

The pyro wire protocol message.

class Pyro4.message.Message(msgType, databytes, serializer_id, flags, seq, annotations=None, hmac_key=None)

Pyro write protocol message.

Wire messages contains of a fixed size header, an optional set of annotation chunks, and then the payload data. This class doesn’t deal with the payload data: (de)serialization and handling of that data is done elsewhere. Annotation chunks are only parsed, except the ‘HMAC’ chunk: that is created and validated because it is used as a message digest.

The header format is:

4   id ('PYRO')
2   protocol version
2   message type
2   message flags
2   sequence number
4   data length   (i.e. 2 Gb data size limitation)
2   data serialization format (serializer id)
2   annotations length (total of all chunks, 0 if no annotation chunks present)
2   (reserved)
2   checksum

After the header, zero or more annotation chunks may follow, of the format:

4   id (ASCII)
2   chunk length
x   annotation chunk databytes

After that, the actual payload data bytes follow.

The sequencenumber is used to check if response messages correspond to the actual request message. This prevents the situation where Pyro would perhaps return the response data from another remote call (which would not result in an error otherwise!) This could happen for instance if the socket data stream gets out of sync, perhaps due To some form of signal that interrupts I/O.

The header checksum is a simple sum of the header fields to make reasonably sure that we are dealing with an actual correct PYRO protocol header and not some random data that happens to start with the ‘PYRO’ protocol identifier.

Pyro now uses two annotation chunks that you should not touch yourself: ‘HMAC’ contains the hmac digest of the message data bytes and all of the annotation chunk data bytes (except those of the HMAC chunk itself). ‘CORR’ contains the correlation id (guid bytes) Other chunk names are free to use for custom purposes, but Pyro has the right to reserve more of them for internal use in the future.

decompress_if_needed()

Decompress the message data if it is compressed.

classmethod from_header(headerData)

Parses a message header. Does not yet process the annotations chunks and message data.

hmac()

returns the hmac of the data and the annotation chunk values (except HMAC chunk itself)

static ping(pyroConnection, hmac_key=None)

Convenience method to send a ‘ping’ message and wait for the ‘pong’ response

classmethod recv(connection, requiredMsgTypes=None, hmac_key=None)

Receives a pyro message from a given connection. Accepts the given message types (None=any, or pass a sequence). Also reads annotation chunks and the actual payload data. Validates a HMAC chunk if present.

send(connection)

send the message as bytes over the connection

to_bytes()

creates a byte stream containing the header followed by annotations (if any) followed by the data

Pyro4.message.secure_compare(a, b, /)

Return ‘a == b’.

This function uses an approach designed to prevent timing analysis, making it appropriate for cryptography.

a and b must both be of the same type: either str (ASCII only), or any bytes-like object.

Note: If a and b are of different lengths, or if an error occurs, a timing attack could theoretically reveal information about the types and lengths of a and b–but not their values.

MSG_*

(int) The various message type identifiers

FLAGS_*

(int) Various bitflags that specify the characteristics of the message, can be bitwise or-ed together