Message

Implementation of an SSH2 “message”.

class paramiko.message.Message(content=None)

An SSH2 message is a stream of bytes that encodes some combination of strings, integers, bools, and infinite-precision integers. This class builds or breaks down such a byte stream.

Normally you don’t need to deal with anything this low-level, but it’s exposed for people implementing custom extensions, or features that paramiko doesn’t support yet.

__init__(content=None)

Create a new SSH2 message.

Parameters

content (bytes) – the byte stream to use as the message content (passed in only when decomposing a message).

__repr__()

Returns a string representation of this object, for debugging.

__weakref__

list of weak references to the object (if defined)

add(*seq)

Add a sequence of items to the stream. The values are encoded based on their type: bytes, str, int, bool, or list.

Warning

Longs are encoded non-deterministically. Don’t use this method.

Parameters

seq – the sequence of items

add_adaptive_int(n)

Add an integer to the stream.

Parameters

n (int) – integer to add

add_boolean(b)

Add a boolean value to the stream.

Parameters

b (bool) – boolean value to add

add_byte(b)

Write a single byte to the stream, without any formatting.

Parameters

b (bytes) – byte to add

add_bytes(b)

Write bytes to the stream, without any formatting.

Parameters

b (bytes) – bytes to add

add_int(n)

Add an integer to the stream.

Parameters

n (int) – integer to add

add_int64(n)

Add a 64-bit int to the stream.

Parameters

n (int) – long int to add

add_list(l)

Add a list of strings to the stream. They are encoded identically to a single string of values separated by commas. (Yes, really, that’s how SSH2 does it.)

Parameters

l – list of strings to add

add_mpint(z)

Add a long int to the stream, encoded as an infinite-precision integer. This method only works on positive numbers.

Parameters

z (int) – long int to add

add_string(s)

Add a bytestring to the stream.

Parameters

s (byte) – bytestring to add

asbytes()

Return the byte stream content of this Message, as a bytes.

get_adaptive_int()

Fetch an int from the stream.

Returns

a 32-bit unsigned int.

get_binary()

Alias for get_string (obtains a bytestring).

get_boolean()

Fetch a boolean from the stream.

get_byte()

Return the next byte of the message, without decomposing it. This is equivalent to get_bytes(1).

Returns

the next (bytes) byte of the message, or b'\' if there aren’t any bytes remaining.

get_bytes(n)

Return the next n bytes of the message, without decomposing into an int, decoded string, etc. Just the raw bytes are returned. Returns a string of n zero bytes if there weren’t n bytes remaining in the message.

get_int()

Fetch an int from the stream.

get_int64()

Fetch a 64-bit int from the stream.

Returns

a 64-bit unsigned integer (int).

get_list()

Fetch a list of strings from the stream.

These are trivially encoded as comma-separated values in a string.

get_mpint()

Fetch a long int (mpint) from the stream.

Returns

an arbitrary-length integer (int).

get_remainder()

Return the bytes of this message that haven’t already been parsed and returned.

get_so_far()

Returns the bytes of this message that have been parsed and returned. The string passed into a message’s constructor can be regenerated by concatenating get_so_far and get_remainder.

get_string()

Fetch a “string” from the stream. This will actually be a bytes object, and may contain unprintable characters. (It’s not unheard of for a string to contain another byte-stream message.)

get_text()

Fetch a Unicode string from the stream.

This currently operates by attempting to encode the next “string” as utf-8.

rewind()

Rewind the message to the beginning as if no items had been parsed out of it yet.