LonamiWebs / Telethon

Pure Python 3 MTProto API Telegram client library, for bots too!
https://docs.telethon.dev
MIT License
9.72k stars 1.39k forks source link

help with: OSError: [Errno 113] No route to host #576

Closed renanfilipe closed 6 years ago

renanfilipe commented 6 years ago

hello, I am trying to build a code which must runs 24/7 and it will redirect all messages from specific channels to one of my channels, here is the code:

from telethon import TelegramClient
from telethon.tl.types import *

api_id = my_api_id
api_hash = my_api_hash

def callback(update):
    if isinstance(update, UpdateNewChannelMessage) and not update.message.out:
        try:
            channel = client.get_entity(update.message.to_id)
            if channel.id == group1 or channel.id == group2 or channel.id == group3:
                message = channel.title + "\n" + update.message.message
                client.send_message(destination, message)
        except AttributeError:
            pass

client = TelegramClient('session_name', api_id, api_hash, update_workers=1, spawn_read_thread=False, save_entities=False)
client.connect()

destination = client.get_entity(PeerChat(myChannelId))

client.add_update_handler(callback)
client.idle()

I had some memory run issues that I think were fixed by adding the save_entities=False on the constructor. Now I got the following error:

Traceback (most recent call last):
 File "/home/altmanager/PycharmProjects/untitled/venv/lib/python3.5/site-packages/telethon/extensions/tcp_client.py", line 150, in read
   partial = self._socket.recv(bytes_left)
OSError: [Errno 22] Invalid argument

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
 File "/home/altmanager/PycharmProjects/untitled/venv/lib/python3.5/site-packages/telethon/telegram_bare_client.py", line 659, in idle
   self._sender.receive(update_state=self.updates)
 File "/home/altmanager/PycharmProjects/untitled/venv/lib/python3.5/site-packages/telethon/network/mtproto_sender.py", line 130, in receive
   body = self.connection.recv()
 File "/home/altmanager/PycharmProjects/untitled/venv/lib/python3.5/site-packages/telethon/network/connection.py", line 184, in _recv_tcp_full
   packet_len_seq = self.read(8)  # 4 and 4
 File "/home/altmanager/PycharmProjects/untitled/venv/lib/python3.5/site-packages/telethon/network/connection.py", line 295, in _read_plain
   return self.conn.read(length)
 File "/home/altmanager/PycharmProjects/untitled/venv/lib/python3.5/site-packages/telethon/extensions/tcp_client.py", line 157, in read
   self._raise_connection_reset()
 File "/home/altmanager/PycharmProjects/untitled/venv/lib/python3.5/site-packages/telethon/extensions/tcp_client.py", line 174, in _raise_connection_reset
   raise ConnectionResetError('The server has closed the connection.')
ConnectionResetError: The server has closed the connection.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
 File "/home/altmanager/PycharmProjects/telegram-chat-reader/main.py", line 38, in <module>
   client.idle()
 File "/home/altmanager/PycharmProjects/untitled/venv/lib/python3.5/site-packages/telethon/telegram_bare_client.py", line 668, in idle
   while self._user_connected and not self._reconnect():
 File "/home/altmanager/PycharmProjects/untitled/venv/lib/python3.5/site-packages/telethon/telegram_bare_client.py", line 277, in _reconnect
   return self.connect()
 File "/home/altmanager/PycharmProjects/untitled/venv/lib/python3.5/site-packages/telethon/telegram_bare_client.py", line 192, in connect
   self._sender.connect()
 File "/home/altmanager/PycharmProjects/untitled/venv/lib/python3.5/site-packages/telethon/network/mtproto_sender.py", line 62, in connect
   self.connection.connect(self.session.server_address, self.session.port)
 File "/home/altmanager/PycharmProjects/untitled/venv/lib/python3.5/site-packages/telethon/network/connection.py", line 100, in connect
   self.conn.connect(ip, port)
 File "/home/altmanager/PycharmProjects/untitled/venv/lib/python3.5/site-packages/telethon/extensions/tcp_client.py", line 75, in connect
   self._socket.connect(address)
OSError: [Errno 113] No route to host

Process finished with exit code 1

I have no idea of what caused it so I appreciate if you can tell me what may happened and how I can handle it.

Lonami commented 6 years ago

This error seems to be different from the one you get when you're disconnected from the network:

Traceback (most recent call last):
  File "/home/lonami/Documents/Projects/Python/PyCharm/Telethon/example.py", line 26, in <module>
    assert client.connect()
  File "/home/lonami/Documents/Projects/Python/PyCharm/Telethon/telethon/telegram_bare_client.py", line 192, in connect
    self._sender.connect()
  File "/home/lonami/Documents/Projects/Python/PyCharm/Telethon/telethon/network/mtproto_sender.py", line 60, in connect
    self.connection.connect(self.session.server_address, self.session.port)
  File "/home/lonami/Documents/Projects/Python/PyCharm/Telethon/telethon/network/connection.py", line 100, in connect
    self.conn.connect(ip, port)
  File "/home/lonami/Documents/Projects/Python/PyCharm/Telethon/telethon/extensions/tcp_client.py", line 70, in connect
    self._socket.connect(address)
OSError: [Errno 101] Network is unreachable

But I have never found OSError: [Errno 113] No route to host and I'm not sure what it means. Neither I know what it can mean by the following:

   partial = self._socket.recv(bytes_left)
OSError: [Errno 22] Invalid argument

I had some memory run issues that I think were fixed by adding the save_entities=False on the constructor.

That's strange and shouldn't at all cause the error you described. If you're using recent versions, entities will be saved into a disk-based SQLite database so memory shouldn't be an issue at all. I ran with save_entities=False and I didn't get such error.

Lonami commented 6 years ago

May I ask why did you close?

renanfilipe commented 6 years ago

sure, my boss was running the script in a very old laptop and according to him the wifi driver was outdated and he also had problems with the quality of the wifi signal. so I tested the script in other computers and now it is working fine. thanks for your patience in helping me

Lonami commented 6 years ago

Haha nice story. Alright glad it works :)

Bersam commented 6 years ago

I have same problem, but inside a docker container. recreating container fixed it. not sure what was the actual problem.

rajatgsap commented 11 months ago

Hey @Bersam, I am also facing the same issues, how did you solved in production environment?, in my case, I have to recreate the session (I am using telemongo, https://pypi.org/project/telemongo/) to save the session. So everytime I need to recreate this session if I face this issue. I am using docker & Sanic to host my application endpoints. @Lonami can you please look into this?