Telethon: "Cannot send requests while disconnected" error when trying to download all media from Channel

I try to write code to download all messages and media from specific channel. Getting messages without downloading media works fine. But with file download it is failed usually earlier than on 10 iteration. I use Python 3.6, Telethon 1.2.

My code:

import telethon

# change on your api id and hash
api_id = 123
api_hash = 'some_api_hash'

client = telethon.TelegramClient('anon', api_id, api_hash)

async def run():  
    await client.connect()
    channel = await client.get_entity('varlamov')

    i = 0
    async for message in client.iter_messages(channel):
        i = i + 1
        print(str(i))

        await client.download_media(message, file='download/')

loop = asyncio.get_event_loop()
loop.run_until_complete(run())

Error:

future: <Task finished coro=<MTProtoSender._recv_loop() done, defined at /usr/local/lib/python3.6/dist-packages/telethon/network/mtprotosender.py:415> exception=CancelledError()>
concurrent.futures._base.CancelledError
Traceback (most recent call last):
  File "app.py", line 25, in <module>
    loop.run_until_complete(run())
  File "/usr/lib/python3.6/asyncio/base_events.py", line 468, in run_until_complete
    return future.result()
  File "app.py", line 20, in run
    await client.download_media(message, file='download/')
  File "/usr/local/lib/python3.6/dist-packages/telethon/client/downloads.py", line 142, in download_media
    media, file, date, progress_callback
  File "/usr/local/lib/python3.6/dist-packages/telethon/client/downloads.py", line 311, in _download_photo
    progress_callback=progress_callback)
  File "/usr/local/lib/python3.6/dist-packages/telethon/client/downloads.py", line 244, in download_file
    input_location, offset, part_size
  File "/usr/local/lib/python3.6/dist-packages/telethon/network/mtprotosender.py", line 202, in send
    raise ConnectionError('Cannot send requests while disconnected')
ConnectionError: Cannot send requests while disconnected```

Also I am trying to catch exception to check whether client is connected or not. And method is_connected() return True.

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 6
  • Comments: 17 (10 by maintainers)

Commits related to this issue

Most upvoted comments

@dmitrdv with Telethon==1.19.5 everything works well
try code bellow:

import telethon

api_id = <your id>
api_hash = '<your hash>'

client = telethon.TelegramClient('anon', api_id, api_hash)

async def run():
    await client.connect()
    channel = await client.get_entity('varlamov')

    i = 0
    async for message in client.iter_messages(channel):
        i = i + 1
        print(str(i))

        await client.download_media(message, file='download/')

client.loop.run_until_complete(run())

is sync run account, and banned. but sync is connect. not disconnect