@xeguro

Проблема с proxy на pythonanywhere, поможете?

import os
import sys
import telepot
import urllib3
import colorama
from aiogram import executor, Dispatcher

from tgbot.data.config import get_admins
from tgbot.data.loader import scheduler
from tgbot.handlers import dp
from tgbot.middlewares import setup_middlewares
from tgbot.services.api_session import AsyncSession
from tgbot.services.api_sqlite import create_dbx
from tgbot.utils.misc.bot_commands import set_commands
from tgbot.utils.misc.bot_logging import bot_logger
from tgbot.utils.misc_functions import check_update, check_bot_data, on_startup_notify, update_profit_day, \
    update_profit_week, autobackup_admin, check_mail

colorama.init()

proxy_url = "http://proxy.server:3128"
telepot.api._pools = {
    'default': urllib3.ProxyManager(proxy_url=proxy_url, num_pools=3, maxsize=10, retries=False, timeout=30),
}
telepot.api._onetime_pool_spec = (urllib3.ProxyManager, dict(proxy_url=proxy_url, num_pools=1, maxsize=1, retries=False, timeout=30))

# Запуск шедулеров
async def scheduler_start(aSession):
    scheduler.add_job(update_profit_week, "cron", day_of_week="mon", hour=00, minute=1)
    scheduler.add_job(update_profit_day, "cron", hour=00)
    scheduler.add_job(check_update, "cron", hour=00, args=(aSession,))
    scheduler.add_job(check_mail, "cron", hour=12, args=(aSession,))
    scheduler.add_job(autobackup_admin, "cron", hour=00)


# Выполнение функции после запуска бота
async def on_startup(dp: Dispatcher):
    aSession = AsyncSession()

    dp.bot['aSession'] = aSession
    await dp.bot.delete_webhook()
    await dp.bot.get_updates(offset=-1)

    await set_commands(dp)
    await check_bot_data()
    await scheduler_start(aSession)
    await on_startup_notify(dp, aSession)

    bot_logger.warning("BOT WAS STARTED")
    print(colorama.Fore.LIGHTYELLOW_EX + "~~~~~ Bot was started ~~~~~")
    print(colorama.Fore.LIGHTBLUE_EX + "~~~~~ TG developer:  *deleted* ~~~~~")
    print(colorama.Fore.RESET)

    if len(get_admins()) == 0: print("***** ENTER ADMIN ID IN settings.ini *****")


# Выполнение функции после выключения бота
async def on_shutdown(dp: Dispatcher):
    aSession: AsyncSession = dp.bot['aSession']
    await aSession.close()

    await dp.storage.close()
    await dp.storage.wait_closed()
    await (await dp.bot.get_session()).close()

    if sys.platform.startswith("win"):
        os.system("cls")
    else:
        os.system("clear")


if __name__ == "__main__":
    create_dbx()

    scheduler.start()
    setup_middlewares(dp)

    executor.start_polling(dp, on_startup=on_startup, on_shutdown=on_shutdown)


Ошибка
Traceback (most recent call last):
  File "/home/xeguro/.local/lib/python3.10/site-packages/aiohttp/connector.py", line 980, in _wrap_create_connection
    return await self._loop.create_connection(*args, **kwargs)  # type: ignore[return-value]  # noqa
  File "/usr/local/lib/python3.10/asyncio/base_events.py", line 1064, in create_connection
    raise exceptions[0]
  File "/usr/local/lib/python3.10/asyncio/base_events.py", line 1049, in create_connection
    sock = await self._connect_sock(
  File "/usr/local/lib/python3.10/asyncio/base_events.py", line 960, in _connect_sock
    await self.sock_connect(sock, address)
  File "/usr/local/lib/python3.10/asyncio/selector_events.py", line 500, in sock_connect
    return await fut
  File "/usr/local/lib/python3.10/asyncio/selector_events.py", line 505, in _sock_connect
    sock.connect(address)
OSError: [Errno 101] Network is unreachable

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/xeguro/.local/lib/python3.10/site-packages/aiogram/bot/api.py", line 139, in make_request
    async with session.post(url, data=req, **kwargs) as response:
  File "/home/xeguro/.local/lib/python3.10/site-packages/aiohttp/client.py", line 1141, in __aenter__
    self._resp = await self._coro
  File "/home/xeguro/.local/lib/python3.10/site-packages/aiohttp/client.py", line 536, in _request
    conn = await self._connector.connect(
  File "/home/xeguro/.local/lib/python3.10/site-packages/aiohttp/connector.py", line 540, in connect
    proto = await self._create_connection(req, traces, timeout)
  File "/home/xeguro/.local/lib/python3.10/site-packages/aiohttp/connector.py", line 901, in _create_connection
    _, proto = await self._create_direct_connection(req, traces, timeout)
  File "/home/xeguro/.local/lib/python3.10/site-packages/aiohttp/connector.py", line 1206, in _create_direct_connection
    raise last_exc
  File "/home/xeguro/.local/lib/python3.10/site-packages/aiohttp/connector.py", line 1175, in _create_direct_connection
    transp, proto = await self._wrap_create_connection(
  File "/home/xeguro/.local/lib/python3.10/site-packages/aiohttp/connector.py", line 988, in _wrap_create_connection
    raise client_error(req.connection_key, exc) from exc
aiohttp.client_exceptions.ClientConnectorError: Cannot connect to host api.telegram.org:443 ssl:default [Network is unreachable]

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/xeguro/.local/lib/python3.10/site-packages/aiogram/utils/executor.py", line 320, in start_polling
    loop.run_until_complete(self._startup_polling())
  File "/usr/local/lib/python3.10/asyncio/base_events.py", line 646, in run_until_complete
    return future.result()
  File "/home/xeguro/.local/lib/python3.10/site-packages/aiogram/utils/executor.py", line 372, in _startup_polling
    await self._welcome()
  File "/home/xeguro/.local/lib/python3.10/site-packages/aiogram/utils/executor.py", line 361, in _welcome
    user = await self.dispatcher.bot.me
  File "/home/xeguro/.local/lib/python3.10/site-packages/aiogram/bot/bot.py", line 30, in me
    setattr(self, '_me', await self.get_me())
  File "/home/xeguro/.local/lib/python3.10/site-packages/aiogram/bot/bot.py", line 233, in get_me
    result = await self.request(api.Methods.GET_ME, payload)
  File "/home/xeguro/.local/lib/python3.10/site-packages/aiogram/bot/base.py", line 236, in request
    return await api.make_request(await self.get_session(), self.server, self.__token, method, data, files,
  File "/home/xeguro/.local/lib/python3.10/site-packages/aiogram/bot/api.py", line 142, in make_request
    raise exceptions.NetworkError(f"aiohttp client throws an error: {e.__class__.__name__}: {e}")
aiogram.utils.exceptions.NetworkError: Aiohttp client throws an error: ClientConnectorError: Cannot connect to host api.telegram.org:443 ssl:default [Network is unreachable]

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/xeguro/main.py", line 79, in <module>
  File "/usr/local/lib/python3.10/asyncio/selector_events.py", line 500, in sock_connect
    executor.start_polling(dp, on_startup=on_startup, on_shutdown=on_shutdown)
  File "/home/xeguro/.local/lib/python3.10/site-packages/aiogram/utils/executor.py", line 45, in start_polling
    executor.start_polling(
  File "/home/xeguro/.local/lib/python3.10/site-packages/aiogram/utils/executor.py", line 328, in start_polling
    loop.run_until_complete(self._shutdown_polling())
  File "/usr/local/lib/python3.10/asyncio/base_events.py", line 646, in run_until_complete
    return future.result()
  File "/home/xeguro/.local/lib/python3.10/site-packages/aiogram/utils/executor.py", line 381, in _shutdown_polling
    await callback(self.dispatcher)
  File "/home/xeguro/main.py", line 60, in on_shutdown
    aSession: AsyncSession = dp.bot['aSession']
  File "/home/xeguro/.local/lib/python3.10/site-packages/aiogram/utils/mixins.py", line 17, in __getitem__
    return self.data[item]
KeyError: 'aSession'


я что-то слышал про то что надо как-то там прокси вставить из-за правила пользования, только вот я не понял чё и куда, также у меня дофига директорий, могу если что скинуть куда-то если знаете в чём заключается ошибка и как её решить
  • Вопрос задан
  • 92 просмотра
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы