Do not connect to Telegram until .connect() is invoked

This commit is contained in:
Lonami Exo 2017-04-06 18:16:15 +02:00
parent 4734d4a7f9
commit ceca636bb1

View File

@ -71,8 +71,8 @@ class TelegramClient:
raise ValueError( raise ValueError(
'The given session must either be a string or a Session instance.') 'The given session must either be a string or a Session instance.')
self.transport = TcpTransport(self.session.server_address, self.transport = None
self.session.port, proxy) self.proxy = proxy # Will be used when a TcpTransport is created
# These will be set later # These will be set later
self.dc_options = None self.dc_options = None
@ -90,6 +90,10 @@ class TelegramClient:
"""Connects to the Telegram servers, executing authentication if required. """Connects to the Telegram servers, executing authentication if required.
Note that authenticating to the Telegram servers is not the same as authenticating Note that authenticating to the Telegram servers is not the same as authenticating
the app, which requires to send a code first.""" the app, which requires to send a code first."""
if self.transport is None:
self.transport = TcpTransport(self.session.server_address,
self.session.port, proxy=self.proxy)
try: try:
if not self.session.auth_key or (reconnect and self.sender is not None): if not self.session.auth_key or (reconnect and self.sender is not None):
self.session.auth_key, self.session.time_offset = \ self.session.auth_key, self.session.time_offset = \
@ -136,7 +140,7 @@ class TelegramClient:
dc = next(dc for dc in self.dc_options if dc.id == dc_id) dc = next(dc for dc in self.dc_options if dc.id == dc_id)
self.transport.close() self.transport.close()
self.transport = TcpTransport(dc.ip_address, dc.port) self.transport = None
self.session.server_address = dc.ip_address self.session.server_address = dc.ip_address
self.session.port = dc.port self.session.port = dc.port
self.session.save() self.session.save()
@ -148,6 +152,9 @@ class TelegramClient:
if self.sender: if self.sender:
self.sender.disconnect() self.sender.disconnect()
self.sender = None self.sender = None
if self.transport:
self.transport.close()
self.transport = None
# endregion # endregion