Messages Module

py2p.messages.compress(msg, method)[source]

Shortcut method for compression

Parameters:
  • msg – The message you wish to compress, the type required is defined by the requested method
  • method

    The compression method you wish to use. Supported (assuming installed):

Returns:

Defined by the compression method, but typically the bytes of the compressed message

Warning

The types fed are dependent on which compression method you use. Best to assume most values are bytes or bytearray

Raises:ValueError – if there is an unknown compression method, or a method-specific error
py2p.messages.decompress(msg, method)[source]

Shortcut method for decompression

Parameters:
  • msg – The message you wish to decompress, the type required is defined by the requested method
  • method

    The decompression method you wish to use. Supported (assuming installed):

Returns:

Defined by the decompression method, but typically the bytes of the compressed message

Warning

The types fed are dependent on which decompression method you use. Best to assume most values are bytes or bytearray

Raises:ValueError – if there is an unknown compression method, or a method-specific error
class py2p.messages.InternalMessage(msg_type, sender, payload, compression=None, timestamp=None)[source]

An object used to build and parse protocol-defined message structures

classmethod feed_string(string, sizeless=False, compressions=None)[source]

Constructs a InternalMessage from a string or bytes object.

Parameters:
  • string – The string you wish to parse
  • sizeless – A bool which describes whether this string has its size header (default: it does)
  • compressions – A iterable containing the standardized compression methods this message might be under (default: [])
Returns:

A InternalMessage from the given string

Raises:
__init__(msg_type, sender, payload, compression=None, timestamp=None)[source]

Initializes a InternalMessage instance

Parameters:
  • msg_type – A header for the message you wish to send
  • sender – A sender ID the message is using
  • payload – An iterable of objects containing the payload of the message
  • compression – A list of the compression methods this message may use (default: [])
  • timestamp – The current UTC timestamp (as an int) (default: result of py2p.utils.getUTC())
Raises:

TypeError – If you feed an object which cannot be fed to msgpack

compression_fail
payload

Returns a tuple containing the message payload encoded as bytes

compression_used

Returns the compression method this message is using

msg_type
sender
compression
time
time_58

Returns this message’s timestamp in base_58

id

Returns the message id

packets

Returns the full tuple of packets in this message encoded as bytes, excluding the header

string

Returns a bytes representation of the message

Raises:TypeError – See _InternalMessage__non_len_string()