diff --git a/telethon/network/authenticator.py b/telethon/network/authenticator.py index 7204433d..4414066c 100755 --- a/telethon/network/authenticator.py +++ b/telethon/network/authenticator.py @@ -11,6 +11,7 @@ def do_authentication(transport): """Executes the authentication process with the Telegram servers. If no error is rose, returns both the authorization key and the time offset""" sender = MtProtoPlainSender(transport) + sender.connect() # Step 1 sending: PQ Request nonce = os.urandom(16) @@ -184,6 +185,9 @@ def do_authentication(transport): # Step 3 response: Complete DH Exchange with BinaryReader(sender.receive()) as reader: + # Everything read from the server, disconnect now + sender.disconnect() + code = reader.read_int(signed=False) if code == 0x3bcbf734: # DH Gen OK nonce_from_server = reader.read(16) diff --git a/telethon/network/mtproto_plain_sender.py b/telethon/network/mtproto_plain_sender.py index 1fa1fbc1..1721d2a3 100755 --- a/telethon/network/mtproto_plain_sender.py +++ b/telethon/network/mtproto_plain_sender.py @@ -12,8 +12,13 @@ class MtProtoPlainSender: self._time_offset = 0 self._last_msg_id = 0 self._transport = transport + + def connect(self): self._transport.connect() + def disconnect(self): + self._transport.close() + def send(self, data): """Sends a plain packet (auth_key_id = 0) containing the given message body (data)""" with BinaryWriter() as writer: diff --git a/telethon/network/mtproto_sender.py b/telethon/network/mtproto_sender.py index 15cbc287..5c31ded6 100644 --- a/telethon/network/mtproto_sender.py +++ b/telethon/network/mtproto_sender.py @@ -46,8 +46,6 @@ class MtProtoSender: self.updates_thread_sleep = None self.updates_thread = None # Set later - self.connect() - def connect(self): """Connects to the server""" self.transport.connect() diff --git a/telethon/telegram_client.py b/telethon/telegram_client.py index 32bd2c9b..d0e44a30 100644 --- a/telethon/telegram_client.py +++ b/telethon/telegram_client.py @@ -99,6 +99,7 @@ class TelegramClient: self.session.save() self.sender = MtProtoSender(self.transport, self.session) + self.sender.connect() # Now it's time to send an InitConnectionRequest # This must always be invoked with the layer we'll be using