telethon.network package

telethon.network.authenticator module

This module contains several functions that authenticate the client machine with Telegram’s servers, effectively creating an authorization key.

telethon.network.authenticator.do_authentication(connection, retries=5)

Performs the authentication steps on the given connection. Raises an error if all attempts fail.

Parameters:
  • connection – the connection to be used (must be connected).
  • retries – how many times should we retry on failure.
Returns:

telethon.network.authenticator.get_int(byte_array, signed=True)

Gets the specified integer from its byte array. This should be used by this module alone, as it works with big endian.

Parameters:
  • byte_array – the byte array representing th integer.
  • signed – whether the number is signed or not.
Returns:

the integer representing the given byte array.

telethon.network.connection module

This module holds both the Connection class and the ConnectionMode enum, which specifies the protocol to be used by the Connection.

class telethon.network.connection.Connection(mode=<ConnectionMode.TCP_FULL: 1>, proxy=None, timeout=datetime.timedelta(0, 5))

Bases: object

Represents an abstract connection (TCP, TCP abridged…). ‘mode’ must be any of the ConnectionMode enumeration.

Note that ‘.send()’ and ‘.recv()’ refer to messages, which will be packed accordingly, whereas ‘.write()’ and ‘.read()’ work on plain bytes, with no further additions.

clone()

Creates a copy of this Connection.

close()

Closes the connection.

connect(ip, port)

Estabilishes a connection to IP:port.

Parameters:
  • ip – the IP to connect to.
  • port – the port to connect to.
get_timeout()

Returns the timeout used by the connection.

is_connected()

Determines whether the connection is alive or not.

Returns:true if it’s connected.
read(length)
recv()

Receives and unpacks a message

send(message)

Encapsulates and sends the given message

write(data)
class telethon.network.connection.ConnectionMode

Bases: enum.Enum

Represents which mode should be used to stabilise a connection.

TCP_FULL: Default Telegram mode. Sends 12 additional bytes and
needs to calculate the CRC value of the packet itself.
TCP_INTERMEDIATE: Intermediate mode between TCP_FULL and TCP_ABRIDGED.
Always sends 4 extra bytes for the packet length.
TCP_ABRIDGED: This is the mode with the lowest overhead, as it will
only require 1 byte if the packet length is less than 508 bytes (127 << 2, which is very common).
TCP_OBFUSCATED: Encodes the packet just like TCP_ABRIDGED, but encrypts
every message with a randomly generated key using the AES-CTR mode so the packets are harder to discern.
TCP_ABRIDGED = 3
TCP_FULL = 1
TCP_INTERMEDIATE = 2
TCP_OBFUSCATED = 4

telethon.network.mtproto_plain_sender module

This module contains the class used to communicate with Telegram’s servers in plain text, when no authorization key has been created yet.

class telethon.network.mtproto_plain_sender.MtProtoPlainSender(connection)

Bases: object

MTProto Mobile Protocol plain sender (https://core.telegram.org/mtproto/description#unencrypted-messages)

connect()

Connects to Telegram’s servers.

disconnect()

Disconnects from Telegram’s servers.

receive()

Receives a plain packet from the network.

Returns:the response body.
send(data)

Sends a plain packet (auth_key_id = 0) containing the given message body (data).

Parameters:data – the data to be sent.

telethon.network.mtproto_sender module

This module contains the class used to communicate with Telegram’s servers encrypting every packet, and relies on a valid AuthKey in the used Session.

class telethon.network.mtproto_sender.MtProtoSender(session, connection)

Bases: object

MTProto Mobile Protocol sender (https://core.telegram.org/mtproto/description).

Note that this class is not thread-safe, and calling send/receive from two or more threads at the same time is undefined behaviour. Rationale:

a new connection should be spawned to send/receive requests in parallel, so thread-safety (hence locking) isn’t needed.
connect()

Connects to the server.

disconnect()

Disconnects from the server.

is_connected()

Determines whether the sender is connected or not.

Returns:true if the sender is connected.
receive(update_state)

Receives a single message from the connected endpoint.

This method returns nothing, and will only affect other parts of the MtProtoSender such as the updates callback being fired or a pending request being confirmed.

Any unhandled object (likely updates) will be passed to update_state.process(TLObject).

Parameters:update_state – the UpdateState that will process all the received Update and Updates objects.
send(*requests)

Sends the specified TLObject(s) (which must be requests), and acknowledging any message which needed confirmation.

Parameters:requests – the requests to be sent.