PythonAnywhere Forums

Connection refused

Can anyone tell me what this means and how to fix it? The program I'm running is suppose to log into discord using the discord.py api wrapper. (FYI I've replaced the actual email and password in the client.login function with asterisks)

Traceback (most recent call last):
  File "/home/raider782/discord.py-master/mayobot.py", line 12, in <module>
    client.login('*****','*****')
  File "/home/raider782/discord.py-master/discord/client.py", line 725, in login
    self._create_websocket(gateway.json().get('url'), reconnect=False)
  File "/home/raider782/discord.py-master/discord/client.py", line 430, in _create_websocket
    self.ws.connect()
  File "/home/raider782/.local/lib/python3.4/site-packages/ws4py/client/__init__.py", line 209, in connect
    self.sock.connect(self.bind_addr)
  File "/usr/lib/python3.4/ssl.py", line 841, in connect
    self._real_connect(addr, False)
  File "/usr/lib/python3.4/ssl.py", line 828, in _real_connect
    socket.connect(self, addr)
ConnectionRefusedError: [Errno 111] Connection refused

That looks like the code is trying to access an external site without using the HTTP proxy that free accounts need to use. If you run echo $http_proxy in a bash shell, you'll see the proxy host and port details you need to use, and you'll have to specify those somehow to the discord API wrapper.

Additionally, when you access a site through the proxy it has to be on our whitelist. If the discord API is a public one, I can add it to the list -- just let me know the hostname.

Ah, just saw that Conrad added the site to the whitelist yesterday -- you can ignore the second part of my last post.

Ok, thanks. I'll try that out

Did you ever get this to work? I'm trying to make a discord bot now, and am having the same problem.

Are you specifying the proxy host in your code to connect to it?

That's what I'm trying to figure out how to do

Probably best to see if there's anything in the docs for the Discord client library you're using, or to ask on any forums they have. If you post a link to the specific client library you're using, we can also take a look and see if there's anything.

Well I'm using discord.py, it's downloaded using pip install --user discord.py the docs are here but I can't find anything in there about proxies

Hey, sorry for the late response. I never did get it to work. I'll ask on the discord api server about using proxies, but let me know if you find a solution.

I thought I had found a solution to this, however it's not working... My code for setting up the proxy was:

proxyc = aiohttp.ProxyConnector("http://proxy.server:3128")
client = discord.Client(connector=proxyc)

However, I get this very long error:

INFO:discord.client:on_ready has successfully been registered as an event
INFO:discord.client:on_message has successfully been registered as an event
INFO:discord.client:logging in using static token
INFO:discord.client:token auth returned status code 200
Traceback (most recent call last):
  File "/usr/lib/python3.5/asyncio/base_events.py", line 632, in create_connection
    yield from self.sock_connect(sock, address)
  File "/usr/lib/python3.5/asyncio/futures.py", line 358, in __iter__
    yield self  # This tells Task to wait for completion.
  File "/usr/lib/python3.5/asyncio/tasks.py", line 290, in _wakeup
    future.result()
  File "/usr/lib/python3.5/asyncio/futures.py", line 274, in result
    raise self._exception
  File "/usr/lib/python3.5/asyncio/selector_events.py", line 436, in _sock_connect_cb
    raise OSError(err, 'Connect call failed %s' % (address,))
ConnectionRefusedError: [Errno 111] Connect call failed ('104.16.59.5', 443)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "/home/wnp78/.local/lib/python3.5/site-packages/aiohttp/connector.py", line 581, in _create_connection
    local_addr=self._local_addr)
  File "/usr/lib/python3.5/asyncio/base_events.py", line 654, in create_connection
    ', '.join(str(exc) for exc in exceptions)))
OSError: Multiple exceptions: [Errno 111] Connect call failed ('104.16.58.5', 443), [Errno 111] Connect call failed ('104.16.59.5', 443)
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
  File "/home/wnp78/.local/lib/python3.5/site-packages/aiohttp/connector.py", line 310, in connect
    transport, proto = yield from self._create_connection(req)
  File "/home/wnp78/.local/lib/python3.5/site-packages/aiohttp/connector.py", line 604, in _create_connection
    (req.host, req.port, exc.strerror)) from exc
aiohttp.errors.ClientOSError: [Errno None] Can not connect to discordapp.com:443 [None]
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
  File "banh.py", line 35, in <module>
    client.run('MTkxMTA1NTI4NjY2MTI4Mzg0.Cj1caw.z2uI9jNKBWx-WXw0I_IeQPuqCFU')
  File "/home/wnp78/.local/lib/python3.5/site-packages/discord/client.py", line 496, in run
    self.loop.run_until_complete(self.start(*args, **kwargs))
  File "/usr/lib/python3.5/asyncio/base_events.py", line 337, in run_until_complete
    return future.result()
  File "/usr/lib/python3.5/asyncio/futures.py", line 274, in result
    raise self._exception
  File "/usr/lib/python3.5/asyncio/tasks.py", line 241, in _step
    result = coro.throw(exc)
  File "/home/wnp78/.local/lib/python3.5/site-packages/discord/client.py", line 468, in start
    yield from self.connect()
  File "/home/wnp78/.local/lib/python3.5/site-packages/discord/client.py", line 421, in connect
    self.ws = yield from DiscordWebSocket.from_client(self)
  File "/home/wnp78/.local/lib/python3.5/site-packages/discord/gateway.py", line 193, in from_client
    gateway = yield from get_gateway(client.token, loop=client.loop)
  File "/home/wnp78/.local/lib/python3.5/site-packages/discord/gateway.py", line 123, in get_gateway
    resp = yield from session.get(endpoints.GATEWAY, headers=headers)
  File "/home/wnp78/.local/lib/python3.5/site-packages/aiohttp/client.py", line 529, in __iter__
    resp = yield from self._coro
  File "/home/wnp78/.local/lib/python3.5/site-packages/aiohttp/client.py", line 183, in _request
    conn = yield from self._connector.connect(req)
  File "/home/wnp78/.local/lib/python3.5/site-packages/aiohttp/connector.py", line 320, in connect
    .format(key, exc.strerror)) from exc
aiohttp.errors.ClientOSError: [Errno None] Cannot connect to host discordapp.com:443 ssl:True [Can not connect to discordapp.com:443 [None]]
ERROR:asyncio:Unclosed client session
client_session: <aiohttp.client.ClientSession object at 0x7f40bd09ce80>

Any help would be appreciated

I think the most relevant bit of the stack track is:

  File "/usr/lib/python3.5/asyncio/selector_events.py", line 436, in _sock_connect_cb
    raise OSError(err, 'Connect call failed %s' % (address,))
ConnectionRefusedError: [Errno 111] Connect call failed ('104.16.59.5', 443)

That's definitely a sign that it's trying to connect directly to an outside server, ignoring your proxy settings.

Having a look at the other bit of the stack trace:

  File "/home/wnp78/.local/lib/python3.5/site-packages/aiohttp/client.py", line 183, in _request
    conn = yield from self._connector.connect(req)
  File "/home/wnp78/.local/lib/python3.5/site-packages/aiohttp/connector.py", line 320, in connect
    .format(key, exc.strerror)) from exc
aiohttp.errors.ClientOSError: [Errno None] Cannot connect to host discordapp.com:443 ssl:True [Can not connect to discordapp.com:443 [None]]

It looks like line 320 in aiohttp/connector.py is inside the code for BaseConnector rather than ProxyConnector.

Are you sure that you're using the ProxyConnector in all code paths? That is, is there a chance that somewhere else you're creating a discord.Client without specifying a connector?

I am having the same exact problem. Any help is appreciated !

Traceback (most recent call last):                                                                                                                                                                                
  File "basic_bot.py", line 89, in <module>
    bot.run('MTkzNTYwMzU1NTk3NzEzNDA5.CkZKoQ.8EBKGUzWZDpjvR0_X8cY6Gs-g94')
  File "/home/Yondaime008/discord.py/discord/client.py", line 467, in run
    self.loop.run_until_complete(self.start(*args, **kwargs))
  File "/usr/lib/python3.5/asyncio/base_events.py", line 337, in run_until_complete
    return future.result()
  File "/usr/lib/python3.5/asyncio/futures.py", line 274, in result
    raise self._exception
  File "/usr/lib/python3.5/asyncio/tasks.py", line 241, in _step
    result = coro.throw(exc)
  File "/home/Yondaime008/discord.py/discord/client.py", line 438, in start
    yield from self.login(*args, **kwargs)
  File "/home/Yondaime008/discord.py/discord/client.py", line 364, in login
    yield from getattr(self, '_login_' + str(n))(*args, **kwargs)
  File "/home/Yondaime008/discord.py/discord/client.py", line 298, in _login_1
    yield from self.http.static_login(token, bot=kwargs.pop('bot', True))
  File "/home/Yondaime008/discord.py/discord/http.py", line 195, in static_login
    data = yield from self.get(self.ME)
  File "/home/Yondaime008/discord.py/discord/http.py", line 105, in request
    r = yield from self.session.request(method, url, **kwargs)
  File "/home/Yondaime008/.local/lib/python3.5/site-packages/aiohttp/client.py", line 529, in __iter__
    resp = yield from self._coro
  File "/home/Yondaime008/.local/lib/python3.5/site-packages/aiohttp/client.py", line 183, in _request
    conn = yield from self._connector.connect(req)
  File "/home/Yondaime008/.local/lib/python3.5/site-packages/aiohttp/connector.py", line 320, in connect
    .format(key, exc.strerror)) from exc
aiohttp.errors.ClientOSError: [Errno None] Cannot connect to host discordapp.com:443 ssl:True [Can not connect to discordapp.com:443 [None]]
Unclosed client session
client_session: <aiohttp.client.ClientSession object at 0x7fcfeb305780>

Hi. I had the same problem (and noticed no solution here), so I did some digging. It appears that WebSockets used by discord.py do not support HTTP proxies. This would just magically work with HTTPS, but since the proxy is HTTP it doesn't. That means that, short of rewriting discord.py with HTTP proxy support (by using websocket-client, for example, which supports HTTP proxies), we may be out of luck.

Of course, I've never worked with web sockets before, so I may be incorrect, but that's what I figured out with a cursory inspection. If anyone else more knowledgeable comes around, I'd love to be proven wrong so I can get my discord bot working.

I think you're right that websockets are unlikely to work through our proxy.

Actually, to bring this thread back, I believe it has something to do with the asyncio connecting directly using the IP address rather than the URL. Is this a possibility, and if it is, is there a way to have the IP whitelisted too?

No, sorry, we can't whitelist IPs and I think it's unlikely that websockets will work through the proxy, anyway.

Nvm