Forums

Outbound restriction on PythonAnywhere connections?

I made a simple discord bot to use with my friends, but I can't get it to work Is this some limitation of the free account?

   (mybotenv) 22:05 ~ $ python main.py
2024-03-21 22:21:26 INFO     discord.client logging in using static token
Traceback (most recent call last):
  File "/home/KaikeVitorino/.virtualenvs/mybotenv/lib/python3.10/site-packages/aiohttp/connector.py", line 992, in _wrap_create_connection
    return await self._loop.create_connection(*args, **kwargs)
  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 535, in _sock_connect_cb
    raise OSError(err, f'Connect call failed {address}')
ConnectionRefusedError: [Errno 111] Connect call failed ('XXX.XXX.XXX.XXX', XXX)
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
  File "/home/KaikeVitorino/main.py", line 110, in <module>
    bot.run(os.getenv('TOKEN'))
  File "/home/KaikeVitorino/.virtualenvs/mybotenv/lib/python3.10/site-packages/discord/client.py", line 860, in run
    asyncio.run(runner())
  File "/usr/local/lib/python3.10/asyncio/runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "/usr/local/lib/python3.10/asyncio/base_events.py", line 646, in run_until_complete
    return future.result()
  File "/home/KaikeVitorino/.virtualenvs/mybotenv/lib/python3.10/site-packages/discord/client.py", line 849, in runner
    await self.start(token, reconnect=reconnect)
  File "/home/KaikeVitorino/.virtualenvs/mybotenv/lib/python3.10/site-packages/discord/client.py", line 777, in start
    await self.login(token)
  File "/home/KaikeVitorino/.virtualenvs/mybotenv/lib/python3.10/site-packages/discord/client.py", line 612, in login
    data = await self.http.static_login(token)
  File "/home/KaikeVitorino/.virtualenvs/mybotenv/lib/python3.10/site-packages/discord/http.py", line 803, in static_login
    data = await self.request(Route('GET', '/users/@me'))
  File "/home/KaikeVitorino/.virtualenvs/mybotenv/lib/python3.10/site-packages/discord/http.py", line 625, in request
    async with self.__session.request(method, url, **kwargs) as response:
  File "/home/KaikeVitorino/.virtualenvs/mybotenv/lib/python3.10/site-packages/aiohttp/client.py", line 1194, in __aenter__
    self._resp = await self._coro
  File "/home/KaikeVitorino/.virtualenvs/mybotenv/lib/python3.10/site-packages/aiohttp/client.py", line 578, in _request
    conn = await self._connector.connect(
  File "/home/KaikeVitorino/.virtualenvs/mybotenv/lib/python3.10/site-packages/aiohttp/connector.py", line 544, in connect
    proto = await self._create_connection(req, traces, timeout)
  File "/home/KaikeVitorino/.virtualenvs/mybotenv/lib/python3.10/site-packages/aiohttp/connector.py", line 911, in _create_connection
    _, proto = await self._create_direct_connection(req, traces, timeout)
  File "/home/KaikeVitorino/.virtualenvs/mybotenv/lib/python3.10/site-packages/aiohttp/connector.py", line 1235, in _create_direct_connection
    raise last_exc
  File "/home/KaikeVitorino/.virtualenvs/mybotenv/lib/python3.10/site-packages/aiohttp/connector.py", line 1204, in _create_direct_connection
    transp, proto = await self._wrap_create_connection(
  File "/home/KaikeVitorino/.virtualenvs/mybotenv/lib/python3.10/site-packages/aiohttp/connector.py", line 1000, in _wrap_create_connection
    raise client_error(req.connection_key, exc) from exc
aiohttp.client_exceptions.ClientConnectorError: Cannot connect to host discord.com:XXX ssl:default [Connect call failed ('XXX.XXX.XXX.XXX', XXX)]

Free accounts need to connect via our proxy. Could you please share the code where you try to make the connection?

Im not sure if I understood correctly Im a beginner and I dont speak English very well But I think what youre asking for would be this?

import discord
import os
from discord.ext import commands
from dotenv import load_dotenv
from FUNCAO_CicloAtual import *
from FUNCAO_CicloX import *

CHAT_ID = 1206478991960182824

# Inicialização do bot
intents = discord.Intents.all()
bot = commands.Bot(command_prefix='!', intents=intents)

# Evento para executar quando o bot estiver pronto
@bot.event
async def on_ready():
    print(f'Logged in as {bot.user.name}')

# Comando para enviar a mensagem do ciclo atual
@bot.command()
async def Ciclo_Atual(ctx):

@bot.command()
async def Ciclo_X(ctx, numero_ciclo: int):

# Inicialização do bot
load_dotenv()
bot.run(os.getenv('TOKEN'))

Hmm discord should be allowed through the proxy. But maybe the discord library doesnt play nicely with proxies. Is there any way to tell the discord library that it needs to use the proxy?

The script connects to the bot using its access token

load_dotenv()
bot.run(os.getenv('TOKEN'))

Our experience from the past is that discord library does not want to work over proxy, and free accounts do not have direct access to the internet. See https://help.pythonanywhere.com/pages/403ForbiddenError/#proxy-details