Python Dicord bot ConnectionResetError after 20min - python

I coded a discord bot which will verify a licensekey with a wordpress api.
If a User say: !key xxx-xxx-xxx-xxx
The Bot checks if the license is valid via a key and if its valid it will start insert into a table and will reply to the user.
If the license is not valid (Error 404) it will only respond to the user.
The Code is following:
# bot.py
import os
import aiohttp
from urllib.request import urlopen
import json
import mysql.connector
import discord
import logging
from discord.ext import commands
from discord.utils import get
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO, filename="logfile", filemode="a+",
format="%(asctime)-15s %(levelname)-8s %(message)s")
TOKEN ='TOKEN'
GUILD_ID = 'ID'
bot = commands.Bot(command_prefix='!')
#bot.event
async def on_ready():
logging.info('------')
logging.info('Logged in as')
logging.info(bot.user.name)
logging.info(bot.user.id)
logging.info('------')
#bot.event
async def on_member_join(member):
await member.create_dm()
await member.dm_channel.send('bla')
def remove_prefix(str, prefix):
return str.lstrip(prefix)
#bot.command(name='g')
async def rename(ctx, name):
await bot.user.edit(username=name)
#bot.command(name='key', pass_context=True)
async def nine_nine(ctx):
if ctx.channel.id == ctx.author.dm_channel.id:
cs = 'cs_***'
ck = 'ck_5***'
username = ctx.message.author.name
discordMemberID = ctx.message.author.id
userMessage = ctx.message.content
key = remove_prefix(userMessage, '!key ')
server = bot.get_guild(id)
role = discord.utils.get(server.roles, id=id)
member = server.get_member(ctx.message.author.id)
user3 = ctx.message.author.id
logging.info( 'DicordUserId %s'%user3)
session = aiohttp.ClientSession()
r = await session.get('https:/mywebsite.com/wp-json/lmfwc/v2/licenses/activate/' + key + '?' + 'consumer_key=' + ck + '&consumer_secret=' + cs )
logging.info(r.status)
if r.status == 200:
r.close()
logging.info("R**")
print("**")
cnx = mysql.connector.connect(host=....)
if cnx.is_connected():
db_Info = cnx.get_server_info()
logging.info('**')
cursor = cnx.cursor(buffered=True)
logging.info("You're connected to database: ")
cursor.execute(***)
if cursor.rowcount == 0:
***
cursor.execute(***)
cnx.commit()
logging.info(**)
cursor.close()
cnx.close())
if member:
await member.add_roles(role)
await ctx.send("**)#
await ctx.send("**")"
await ctx.send('***')
cursor.close()
cnx.close()
logging.info("Closed Connection.")
else:
logging.error("Conection Error")
await ctx.send('Connection Problems. Please Contact an Administrator of the Server!')
elif r.status == 404:
r.close()
print('HTTPError: {}'.format(e.code))
await ctx.send('***')
else:
logging.warning("Private command only")
await ctx.send("Private command only. Please DM me with the '!key' command :)")
bot.run(TOKEN)
Its running perfectly fine but after about 20 minutes there is like a timeout with the connection and the bot WONT respons at first !key command:
Python 3.7.6 (tags/v3.7.6:43364a7ae0, Dec 19 2019, 00:42:30) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>>
======= RESTART: C:\Users\Administrator\Desktop\Discord Bot\LicenseBot.py ======
Ignoring exception in command key:
Traceback (most recent call last):
File "C:\Program Files\Python37\lib\site-packages\aiohttp\connector.py", line 936, in _wrap_create_connection
return await self._loop.create_connection(*args, **kwargs) # type: ignore # noqa
File "C:\Program Files\Python37\lib\asyncio\base_events.py", line 985, in create_connection
ssl_handshake_timeout=ssl_handshake_timeout)
File "C:\Program Files\Python37\lib\asyncio\base_events.py", line 1013, in _create_connection_transport
await waiter
File "C:\Program Files\Python37\lib\asyncio\selector_events.py", line 814, in _read_ready__data_received
data = self._sock.recv(self.max_size)
ConnectionResetError: [WinError 10054] An existing connection was forcibly closed by the remote host
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Program Files\Python37\lib\site-packages\discord\ext\commands\core.py", line 85, in wrapped
ret = await coro(*args, **kwargs)
File "C:\Users\Administrator\Desktop\Discord Bot\LicenseBot.py", line 72, in nine_nine
r = await session.get('URL + key + '?' + 'consumer_key=' + ck + '&consumer_secret=' + cs )
File "C:\Program Files\Python37\lib\site-packages\aiohttp\client.py", line 483, in _request
timeout=real_timeout
File "C:\Program Files\Python37\lib\site-packages\aiohttp\connector.py", line 523, in connect
proto = await self._create_connection(req, traces, timeout)
File "C:\Program Files\Python37\lib\site-packages\aiohttp\connector.py", line 859, in _create_connection
req, traces, timeout)
File "C:\Program Files\Python37\lib\site-packages\aiohttp\connector.py", line 1004, in _create_direct_connection
raise last_exc
File "C:\Program Files\Python37\lib\site-packages\aiohttp\connector.py", line 986, in _create_direct_connection
req=req, client_error=client_error)
File "C:\Program Files\Python37\lib\site-packages\aiohttp\connector.py", line 943, in _wrap_create_connection
raise client_error(req.connection_key, exc) from exc
aiohttp.client_exceptions.ClientConnectorError: Cannot connect to host URL.com:443 ssl:default [An existing connection was forcibly closed by the remote host]
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Program Files\Python37\lib\site-packages\discord\ext\commands\bot.py", line 903, in invoke
await ctx.command.invoke(ctx)
File "C:\Program Files\Python37\lib\site-packages\discord\ext\commands\core.py", line 855, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "C:\Program Files\Python37\lib\site-packages\discord\ext\commands\core.py", line 94, in wrapped
raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: ClientConnectorError: Cannot connect to host URL.com:443 ssl:default [An existing connection was forcibly closed by the remote host]
After this error I can normally fullfill the !key command again and new users will receive a be welcome message.
Already tried library requests instead of urlib
Tried requests with a session and a session.close() too.
Hosting via AWS EC2. Otehr services running perfectly fine.

discord.ext.commands.errors.CommandInvokeError: Command raised an exception: URLError: <urlopen error [WinError 10054] An existing connection was forcibly closed by the remote host>
As the error says, the connection was closed by the remote host.
Check your internet connection, and the connection of the server.

Since you are using async python you need to use aiohttp. urlopen is not async and will block the event loop.

If you don't think it's your internet connection then Discord closed the connection. Either bad connectivity or blocking code (time.sleep, requests, sync SQL access, intense computation/image processing, or you're just doing things too fast i.e sending too many messages.

Related

wavelink node not connecting or creating pool

so i'm sure many people in the discord py community know of robotic nation on youtube, alexa makes great tutorials i had folower their first music bot tutorial using youtubedl but as it now violated youtube terms of service they made a new tutorial using wavelink and lavalink with java 13, i’ve set it all up to spec per the tutorial but for some reason i keep getting errors about no nodes in pool even after the node pool should be configured correctly to spec from the video
i'm hosting my bot via a rpi4 compute and have java 13.0.1 installed along with wavelink for this project bellow are the codes used i’ve messaged alexa and tried to trouble shoot with them multiple times but they cant even spot the error either hopefully someomne here can spot it
lavalink.jar is a dependency used
application.yml
server: # REST and WS server
port: 2333
address: 0.0.0.0
lavalink:
server:
password: "youshallnotpass"
sources:
youtube: true
bandcamp: true
soundcloud: true
twitch: true
vimeo: true
http: true
local: false
bufferDurationMs: 400 # The duration of the NAS buffer. Higher values fare better against longer GC pauses
frameBufferDurationMs: 5000 # How many milliseconds of audio to keep buffered
youtubePlaylistLoadLimit: 6 # Number of pages at 100 each
playerUpdateInterval: 5 # How frequently to send player updates to clients, in seconds
youtubeSearchEnabled: true
soundcloudSearchEnabled: true
gc-warnings: true
#ratelimit:
#ipBlocks: ["1.0.0.0/8", "..."] # list of ip blocks
#excludedIps: ["...", "..."] # ips which should be explicit excluded from usage by lavalink
#strategy: "RotateOnBan" # RotateOnBan | LoadBalance | NanoSwitch | RotatingNanoSwitch
#searchTriggersFail: true # Whether a search 429 should trigger marking the ip as failing
#retryLimit: -1 # -1 = use default lavaplayer value | 0 = infinity | >0 = retry will happen this numbers times
metrics:
prometheus:
enabled: false
endpoint: /metrics
sentry:
dsn: ""
environment: ""
# tags:
# some_key: some_value
# another_key: another_value
logging:
file:
max-history: 30
max-size: 1GB
path: ./logs/
level:
root: INFO
lavalink: INFO
bot.py (section in question)
#bot.command()
async def connect(ctx):
vc = ctx.voice_client
print("----begin debug log ??connect----")
print("vc variable: "+str(vc))
try:
print("attempt to fetch channel author")
channel = ctx.author.voice.channel
print("channel author: "+str(channel))
print("after attempt to fetch channel author")
except:
print("attept to fetch exception AtributeError")
return await ctx.send("please join a voice channel to connect.")
print("after attept to fetch exception AtributeError")
if not vc:
print("before not vc")
await ctx.author.voice.channel.connect(cls=CustomPlayer())
print("after not vc")
else:
print("before else not vc")
await ctx.send("the bot is already connected to a voice channel")
print("after else not vc")
print("----end connect debug log----")
the class that also seems to have shown issues in debug
class CustomPlayer(wavelink.Player):
def __init__(self):
super().__init__()
self.queue = wavelink.Queue()
the error code produced
Traceback (most recent call last):
File "/usr/local/lib/python3.9/dist-packages/discord/ext/commands/bot.py", line 1347, in invoke
await ctx.command.invoke(ctx)
File "/usr/local/lib/python3.9/dist-packages/discord/ext/commands/core.py", line 986, in invoke
await injected(*ctx.args, **ctx.kwargs) # type: ignore
File "/usr/local/lib/python3.9/dist-packages/discord/ext/commands/core.py", line 199, in wrapped
raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: ZeroConnectedNodes: There are no connected Nodes on this pool.
after fixing the code a bit and accommodating the fact that loop is deprecated i still get this error log, i'm not sure why it just wont create nodes this is becoming quite frustrating
Traceback (most recent call last):
File "/usr/local/lib/python3.9/dist-packages/discord/client.py", line 409, in _run_event
await coro(*args, **kwargs)
File "/home/pi/bot/bot.py", line 79, in on_ready
asyncio.run(main())
File "/usr/lib/python3.9/asyncio/runners.py", line 33, in run
raise RuntimeError(
RuntimeError: asyncio.run() cannot be called from a running event loop
/usr/local/lib/python3.9/dist-packages/discord/client.py:416: RuntimeWarning: coroutine 'on_ready.<locals>.main' was never awaited
pass
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
[2022-09-18 16:54:14] [ERROR ] discord.ext.commands.bot: Ignoring exception in command connect
Traceback (most recent call last):
File "/usr/local/lib/python3.9/dist-packages/discord/ext/commands/core.py", line 190, in wrapped
ret = await coro(*args, **kwargs)
File "/home/pi/bot/bot.py", line 234, in connect
await ctx.author.voice.channel.connect(cls=CustomPlayer())
File "/home/pi/bot/bot.py", line 29, in __init__
super().__init__()
File "/usr/local/lib/python3.9/dist-packages/wavelink/player.py", line 89, in __init__
node = NodePool.get_node()
File "/usr/local/lib/python3.9/dist-packages/wavelink/pool.py", line 452, in get_node
raise ZeroConnectedNodes("There are no connected Nodes on this pool.")
wavelink.errors.ZeroConnectedNodes: There are no connected Nodes on this pool.
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/usr/local/lib/python3.9/dist-packages/discord/ext/commands/bot.py", line 1347, in invoke
await ctx.command.invoke(ctx)
File "/usr/local/lib/python3.9/dist-packages/discord/ext/commands/core.py", line 986, in invoke
await injected(*ctx.args, **ctx.kwargs) # type: ignore
File "/usr/local/lib/python3.9/dist-packages/discord/ext/commands/core.py", line 199, in wrapped
raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: ZeroConnectedNodes: There are no connected Nodes on this pool.
fixed code in question
async def connect_nodes():
await bot.wait_until_ready()
await wavelink.NodePool.create_node(
bot=bot,
host='192.168.1.81',
port=2333,
password='JnG-330-qrsd')
async def main():
await connect_nodes() # again, no need to run with AbstractLoopEvent if you can await
await bot.start(TOKEN)
asyncio.run(main())
on another attempt on my current code i'm getting this error trying to launch the bot.py
[H[2J[3J[1;33m launching...[0m
Traceback (most recent call last):
File "/home/pi/bot/bot.py", line 739, in <module>
asyncio.run(main())
File "/usr/lib/python3.9/asyncio/runners.py", line 44, in run
return loop.run_until_complete(main)
File "/usr/lib/python3.9/asyncio/base_events.py", line 642, in run_until_complete
return future.result()
File "/home/pi/bot/bot.py", line 83, in main
await connect_nodes() # again, no need to run with AbstractLoopEvent if you can await
File "/home/pi/bot/bot.py", line 75, in connect_nodes
await bot.wait_until_ready()
File "/usr/local/lib/python3.9/dist-packages/discord/client.py", line 1108, in wait_until_ready
raise RuntimeError(
RuntimeError: Client has not been properly initialised. Please use the login method or asynchronous context manager before calling this method
[1;33m closed....[0m
i'm at a loss
You need to create a node with wavelink that handles the connection to Lavalink
import wavelink
from discord.ext import commands
bot = commands.Bot(command_prefix='?')
#bot.event
async def on_ready():
await wavelink.NodePool.create_node(bot=bot,
host='0.0.0.0',
port=2333,
password='YOUR_LAVALINK_PASSWORD')
bot.run('token')

RuntimeError: Event loop is closed (discord.py)

So I was making discord bot and everything seemed to be successful, until I stopped programm and started getting this error (yes... kinda big):
Traceback (most recent call last):
File "C:\Users\user\PycharmProjects\DsBots\venv\lib\site-packages\discord\http.py", line 300, in static_login
data = await self.request(Route('GET', '/users/#me'))
File "C:\Users\user\PycharmProjects\DsBots\venv\lib\site-packages\discord\http.py", line 254, in request
raise HTTPException(r, data)
discord.errors.HTTPException: 401 Unauthorized (error code: 0): 401: Unauthorized
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\user\PycharmProjects\DsBots\bot.py", line 30, in <module>
client.run(TOKEN)
File "C:\Users\user\PycharmProjects\DsBots\venv\lib\site-packages\discord\client.py", line 723, in run
return future.result()
File "C:\Users\user\PycharmProjects\DsBots\venv\lib\site-packages\discord\client.py", line 702, in runner
await self.start(*args, **kwargs)
File "C:\Users\user\PycharmProjects\DsBots\venv\lib\site-packages\discord\client.py", line 665, in start
await self.login(*args, bot=bot)
File "C:\Users\user\PycharmProjects\DsBots\venv\lib\site-packages\discord\client.py", line 511, in login
await self.http.static_login(token.strip(), bot=bot)
File "C:\Users\user\PycharmProjects\DsBots\venv\lib\site-packages\discord\http.py", line 304, in static_login
raise LoginFailure('Improper token has been passed.') from exc
discord.errors.LoginFailure: Improper token has been passed.
Exception ignored in: <function _ProactorBasePipeTransport.__del__ at 0x0000026E2673BD90>
Traceback (most recent call last):
File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\asyncio\proactor_events.py", line 116, in __del__
self.close()
File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\asyncio\proactor_events.py", line 108, in close
self._loop.call_soon(self._call_connection_lost, None)
File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\asyncio\base_events.py", line 750, in call_soon
self._check_closed()
File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\asyncio\base_events.py", line 515, in _check_closed
raise RuntimeError('Event loop is closed')
RuntimeError: Event loop is closed
So I found out that the problem is in authorization, and maybe security, or maybe an error in code causes bot to logout().
I found many same questions, but many of them was asked because of undetected syntax error.
Any suggestions?...
Edit: Sorry, forgot to post code. Here it is:
import os
import discord
from dotenv import load_dotenv
load_dotenv()
TOKEN = os.getenv("DISCORD_TOKEN")
GUILD = os.getenv("DISCORD_GUILD")
client = discord.Client()
#client.event
async def on_ready():
# print(f'{client.user} has connected to Discord!')
guild = discord.utils.get(client.guilds, name=GUILD)
print(
f'{client.user} is connected to the following guild:\n'
f'{guild.name}(id: {guild.id})\n'
)
members = '\n - '.join([member.name for member in guild.members])
print(f'Guild Members:\n - {members}')
async def on_message(message):
print(message)
client.run(TOKEN)
So the problem was because I was writting my TOKEN and GUILD in .env file in {}/"" (so it looked like DISCORD_TOKEN="token" or DISCORD_TOKEN={token}). But .env file always writes variables in string type. I didn't knew that, so I was getting LoginFailure: Improper token has been passed. (thanks to Eric Jin) because of my TOKEN and GUILD was with {}.
The problem solution was very simple:
Old code (.env file):
DISCORD_TOKEN="token"
DISCORD_GUILD="guild"
New code:
DISCORD_TOKEN=token
DISCORD_GUILD=guild
(yes...)

python connecting to ble device with passkey using bleak

I'm trying to connect to a ble device with bleak that uses a passkey.
async def connect(self, address):
print("Connecting to device...")
async with BleakClient(address) as client:
response = await client.connect()
print(response)
i'm using above code. The device displays the passkey to enter and windows displays the add a device message to input the code, but i get a timeout error from asyncio and never recieve the response to python.
Traceback (most recent call last):
File "C:\Users\halmelam\PycharmProjects\read_passcode\main.py", line 137, in <module>
asyncio.run(pair.connect(device.address))
File "C:\Users\halmelam\AppData\Local\Programs\Python\Python39\lib\asyncio\runners.py", line 44, in run
return loop.run_until_complete(main)
File "C:\Users\halmelam\AppData\Local\Programs\Python\Python39\lib\asyncio\base_events.py", line 642, in run_until_complete
return future.result()
File "C:\Users\halmelam\PycharmProjects\read_passcode\main.py", line 95, in connect
response = await client.connect()
File "C:\Users\halmelam\.virtualenvs\read_passcode\lib\site-packages\bleak\backends\winrt\client.py", line 249, in connect
await asyncio.wait_for(event.wait(), timeout=timeout)
File "C:\Users\halmelam\AppData\Local\Programs\Python\Python39\lib\asyncio\tasks.py", line 494, in wait_for
raise exceptions.TimeoutError() from exc
asyncio.exceptions.TimeoutError
How do I get the response and how can i send the passkey via python?

Asyncpg cannot fetch data from the database

I am writing my API using aiohttp, asycpg and asyncpgsa.
I create my application:
async def create_app():
app = web.Application(client_max_size=1024 ** 2 * 70)
Then I execute these lines:
async def on_start(app):
app['db'] = asyncpgsa.create_pool(dsn="postgresql://127.0.0.1:5432/backend")
async def on_shutdown(app):
app['db'].close()
app.on_startup.append(on_start)
app.on_cleanup.append(on_shutdown)
In general, in the example where I got it from, it is written like this:
app['db'] = await asyncpgsa.create_pool(dsn="postgresql://127.0.0.1:5432/backend")
But if I write like this, then an error is thrown "ConnectionRefusedError: [Errno 10061] Connect call failed ('127.0.0.1', 5432)"
But that's okay. Now, when the user visits the URL I need, this function should be triggered:
async def post(request):
async with request.app["db"].acquire() as conn:
query = select([datab.post])
result = await conn.fetch(query)
The datab file says this:
from sqlalchemy import Table, Text, VARCHAR, Integer, MetaData, Column
meta = MetaData()
post = Table(
"post", meta,
Column("id", Integer, primary_key=True),
Column("title", VARCHAR, nullable=True),
Column("body", Text))
But when I go to the URL I want, the site gives me "500 Internal Server Error Server got itself in trouble "
And Pycharm: Error handling request asyncpg.exceptions._base.InterfaceError: pool is not initialized
Very little has been written on the Internet about asyncpg (sa), so I will be immensely grateful if you can help me fix the problem.
I'll add my own code.
main.py
from aiohttp import web
from demo import create_app
import argparse
import sqlalchemy
import asyncpgsa
async def post_handler(request):
body1 = await request.json()
print(body1)
return web.json_response(data=body1, status=201)
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--host", help="IPv4/IPv6 address API server would listen on", default="0.0.0.0")
parser.add_argument('--port', help='TCP port API server would listen on', default=8080, type=int)
args = parser.parse_args()
app = create_app()
web.run_app(app, host=args.host, port=args.port)
if __name__ == '__main__':
main()
app.py
from aiohttp import web
import asyncpgsa
from .routes import setup_routes
async def on_start(app):
app['db'] = asyncpgsa.create_pool(dsn="postgresql://127.0.0.1:5432/backendyandex")
async def on_shutdown(app):
app['db'].close()
async def create_app():
app = web.Application(client_max_size=1024 ** 2 * 70)
setup_routes(app)
app.on_startup.append(on_start)
app.on_cleanup.append(on_shutdown)
return app
If you write
app['db'] = await asyncpgsa.create_pool(dsn="postgresql://127.0.0.1:5432/backend")
Then an error occurs
unhandled exception during asyncio.run() shutdown
task: <Task finished coro=<_run_app() done, defined at C:\Users\lisgl\Desktop\PycharmProjects\BackendYandex\venv\lib\site-packages\aiohttp\web.py:287> exception=ConnectionRefusedError(10061, "Connect call failed ('127.0.0.1', 5432)")>
Traceback (most recent call last):
File "C:\Users\lisgl\Desktop\PycharmProjects\BackendYandex\venv\lib\site-packages\aiohttp\web.py", line 508, in run_app
loop.run_until_complete(main_task)
File "C:\Users\lisgl\AppData\Local\Programs\Python\Python37\lib\asyncio\base_events.py", line 587, in run_until_complete
return future.result()
File "C:\Users\lisgl\Desktop\PycharmProjects\BackendYandex\venv\lib\site-packages\aiohttp\web.py", line 319, in _run_app
await runner.setup()
File "C:\Users\lisgl\Desktop\PycharmProjects\BackendYandex\venv\lib\site-packages\aiohttp\web_runner.py", line 275, in setup
self._server = await self._make_server()
File "C:\Users\lisgl\Desktop\PycharmProjects\BackendYandex\venv\lib\site-packages\aiohttp\web_runner.py", line 375, in _make_server
await self._app.startup()
File "C:\Users\lisgl\Desktop\PycharmProjects\BackendYandex\venv\lib\site-packages\aiohttp\web_app.py", line 416, in startup
await self.on_startup.send(self)
File "C:\Users\lisgl\Desktop\PycharmProjects\BackendYandex\venv\lib\site-packages\aiohttp\signals.py", line 34, in send
await receiver(*args, **kwargs) # type: ignore
File "C:\Users\lisgl\Desktop\PycharmProjects\BackendYandex\demo\app.py", line 11, in on_start
app['db'] = await asyncpgsa.create_pool(dsn="postgresql://127.0.0.1:5432/backendyandex")
File "C:\Users\lisgl\Desktop\PycharmProjects\BackendYandex\venv\lib\site-packages\asyncpg\pool.py", line 407, in _async__init__
await self._initialize()
File "C:\Users\lisgl\Desktop\PycharmProjects\BackendYandex\venv\lib\site-packages\asyncpg\pool.py", line 435, in _initialize
await first_ch.connect()
File "C:\Users\lisgl\Desktop\PycharmProjects\BackendYandex\venv\lib\site-packages\asyncpg\pool.py", line 127, in connect
self._con = await self._pool._get_new_connection()
File "C:\Users\lisgl\Desktop\PycharmProjects\BackendYandex\venv\lib\site-packages\asyncpg\pool.py", line 482, in _get_new_connection
**self._connect_kwargs)
File "C:\Users\lisgl\Desktop\PycharmProjects\BackendYandex\venv\lib\site-packages\asyncpg\connection.py", line 1997, in connect
max_cacheable_statement_size=max_cacheable_statement_size,
File "C:\Users\lisgl\Desktop\PycharmProjects\BackendYandex\venv\lib\site-packages\asyncpg\connect_utils.py", line 677, in _connect
raise last_error
File "C:\Users\lisgl\Desktop\PycharmProjects\BackendYandex\venv\lib\site-packages\asyncpg\connect_utils.py", line 668, in _connect
record_class=record_class,
File "C:\Users\lisgl\Desktop\PycharmProjects\BackendYandex\venv\lib\site-packages\asyncpg\connect_utils.py", line 634, in _connect_addr
tr, pr = await compat.wait_for(connector, timeout=timeout)
File "C:\Users\lisgl\Desktop\PycharmProjects\BackendYandex\venv\lib\site-packages\asyncpg\compat.py", line 103, in wait_for
return await asyncio.wait_for(fut, timeout)
File "C:\Users\lisgl\AppData\Local\Programs\Python\Python37\lib\asyncio\tasks.py", line 442, in wait_for
return fut.result()
File "C:\Users\lisgl\Desktop\PycharmProjects\BackendYandex\venv\lib\site-packages\asyncpg\connect_utils.py", line 547, in _create_ssl_connection
host, port)
File "C:\Users\lisgl\AppData\Local\Programs\Python\Python37\lib\asyncio\base_events.py", line 962, in create_connection
raise exceptions[0]
File "C:\Users\lisgl\AppData\Local\Programs\Python\Python37\lib\asyncio\base_events.py", line 949, in create_connection
await self.sock_connect(sock, address)
File "C:\Users\lisgl\AppData\Local\Programs\Python\Python37\lib\asyncio\selector_events.py", line 473, in sock_connect
return await fut
File "C:\Users\lisgl\AppData\Local\Programs\Python\Python37\lib\asyncio\selector_events.py", line 503, in _sock_connect_cb
raise OSError(err, f'Connect call failed {address}')
ConnectionRefusedError: [Errno 10061] Connect call failed ('127.0.0.1', 5432)
site.py
from aiohttp import web
from sqlalchemy import select
from . import datab
async def post(request):
async with request.app["db"].acquire() as conn:
query = select([datab.post])
result = await conn.fetch(query)
return web.Response(body=str(result))
And here is the error that crashes if you go to the URL I need:
Error handling request
Traceback (most recent call last):
File "C:\Users\lisgl\Desktop\PycharmProjects\BackendYandex\venv\lib\site-packages\aiohttp\web_protocol.py", line 422, in _handle_request
resp = await self._request_handler(request)
File "C:\Users\lisgl\Desktop\PycharmProjects\BackendYandex\venv\lib\site-packages\aiohttp\web_app.py", line 499, in _handle
resp = await handler(request)
File "C:\Users\lisgl\Desktop\PycharmProjects\BackendYandex\demo\site.py", line 13, in post
async with request.app["db"].acquire() as conn:
File "C:\Users\lisgl\Desktop\PycharmProjects\BackendYandex\venv\lib\site-packages\asyncpg\pool.py", line 785, in __aenter__
self.connection = await self.pool._acquire(self.timeout)
File "C:\Users\lisgl\Desktop\PycharmProjects\BackendYandex\venv\lib\site-packages\asyncpg\pool.py", line 622, in _acquire
self._check_init()
File "C:\Users\lisgl\Desktop\PycharmProjects\BackendYandex\venv\lib\site-packages\asyncpg\pool.py", line 745, in _check_init
raise exceptions.InterfaceError('pool is not initialized')
asyncpg.exceptions._base.InterfaceError: pool is not initialized
Your dsn is incorrect, so you can't connect to your database.
postgres://user:pass#host:port/database?option=value is the correct format. You forgot the user and the password.
Also asyncpgsa.create_pool() should be awaited, if not. You don't get the error, because you only assign a coroutine to the variable app['db']. So the connection pool is also not created.
Your second error (from site.py) is caused by not initizalising the connection pool.
More about that you can find in the documentation of asyncpg here (because asyncpgsa's connection pool is based on asyncpg's connection pool).

aiohttp.client_exceptions.ClientConnectorError: Cannot connect to host stackoverflow.com:443 ssl:default [Connect call failed ('151.101.193.69', 443)]

here is my code:
import asyncio
from aiohttp import ClientSession
async def main():
url = "https://stackoverflow.com/"
async with ClientSession() as session:
async with session.get(url) as resp:
print(resp.status)
asyncio.run(main())
if I run it on my computer, everything works, but if I run it on pythonanywhere, I get this error:
Traceback (most recent call last):
File "/home/0dminnimda/.local/lib/python3.8/site-packages/aiohttp/connector.py", line 936, in _wrap_create_connection
return await self._loop.create_connection(*args, **kwargs) # type: ignore # noqa
File "/usr/lib/python3.8/asyncio/base_events.py", line 1017, in create_connection
raise exceptions[0]
File "/usr/lib/python3.8/asyncio/base_events.py", line 1002, in create_connection
sock = await self._connect_sock(
File "/usr/lib/python3.8/asyncio/base_events.py", line 916, in _connect_sock
await self.sock_connect(sock, address)
File "/usr/lib/python3.8/asyncio/selector_events.py", line 485, in sock_connect
return await fut
File "/usr/lib/python3.8/asyncio/selector_events.py", line 517, in _sock_connect_cb
raise OSError(err, f'Connect call failed {address}')
ConnectionRefusedError: [Errno 111] Connect call failed ('151.101.193.69', 443)
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "test_c.py", line 39, in <module>
asyncio.run(main())
File "/usr/lib/python3.8/asyncio/runners.py", line 43, in run
return loop.run_until_complete(main)
File "/usr/lib/python3.8/asyncio/base_events.py", line 608, in run_until_complete
return future.result()
File "test_c.py", line 28, in main
async with session.get(url, timeout=30) as resp: # , headers=headers
File "/home/0dminnimda/.local/lib/python3.8/site-packages/aiohttp/client.py", line 1012, in __aenter__
self._resp = await self._coro
File "/home/0dminnimda/.local/lib/python3.8/site-packages/aiohttp/client.py", line 480, in _request
conn = await self._connector.connect(
File "/home/0dminnimda/.local/lib/python3.8/site-packages/aiohttp/connector.py", line 523, in connect
proto = await self._create_connection(req, traces, timeout)
File "/home/0dminnimda/.local/lib/python3.8/site-packages/aiohttp/connector.py", line 858, in _create_connection
_, proto = await self._create_direct_connection(
File "/home/0dminnimda/.local/lib/python3.8/site-packages/aiohttp/connector.py", line 1004, in _create_direct_connection
raise last_exc
File "/home/0dminnimda/.local/lib/python3.8/site-packages/aiohttp/connector.py", line 980, in _create_direct_connection
transp, proto = await self._wrap_create_connection(
File "/home/0dminnimda/.local/lib/python3.8/site-packages/aiohttp/connector.py", line 943, in _wrap_create_connection
raise client_error(req.connection_key, exc) from exc
aiohttp.client_exceptions.ClientConnectorError: Cannot connect to host stackoverflow.com:443 ssl:default [Connect call failed ('151.101.193.69', 443)]
Unclosed client session
client_session: <aiohttp.client.ClientSession object at 0x7f25a71d1a90>
aiohttp on hosting:
Name: aiohttp
Version: 3.6.2
Summary: Async http client/server framework (asyncio)
Home-page: https://github.com/aio-libs/aiohttp
Author: Nikolay Kim
Author-email: fafhrd91#gmail.com
License: Apache 2
Location: /home/0dminnimda/.local/lib/python3.8/site-packages
Requires: chardet, async-timeout, multidict, yarl, attrs
Required-by:
aiohttp on my PC:
Name: aiohttp
Version: 3.6.2
Summary: Async http client/server framework (asyncio)
Home-page: https://github.com/aio-libs/aiohttp
Author: Nikolay Kim
Author-email: fafhrd91#gmail.com
License: Apache 2
Location: c:\users\asus\appdata\roaming\python\python38\site-packages
Requires: async-timeout, attrs, chardet, yarl, multidict
Required-by:
I am at a loss that it is not so? I am running both files using python3.8.
I also tried other urls, they have the same problem
Do I need to add any more details?
first solution
Referring to the help from the forum, I added trust_env = True when creating the client and now everything works.
Explanation:
Free accounts on PythonAnywhere must use a proxy to connect to the public internet, but aiohttp, by default, does not connect to a proxy accessible from an environment variable.
Link to aiohttp documentation (look for a parameter called "trust_env")
Here is the new code:
import asyncio
from aiohttp import ClientSession
async def main():
url = "https://stackoverflow.com/"
async with ClientSession(trust_env=True) as session:
async with session.get(url) as resp:
print(resp.status)
asyncio.run(main())
solution if the first didn't help you
The domain you are trying to access must be in whitelist, otherwise you may also get this error.
In this case you need to post a new topic on the pythonanywhere forum asking to add the domain to the whitelist.
If this is an api, then you will need to provide a link to the documentation for this api.
If you are using Windows OS with python (3.8 or newer) and aiohttp (3.7.4 or older)
Sometimes the solution for an exception like ... Cannot connect to host <REQUESTED URL>:443 ssl:default [The parameter is incorrect] is:
import sys
...
policy = asyncio.WindowsSelectorEventLoopPolicy()
asyncio.set_event_loop_policy(policy)
asyncio.run(main())
And you can check your Python version and OS:
import sys
...
if (sys.platform.startswith('win')
and sys.version_info[0] == 3
and sys.version_info[1] >= 8):
policy = asyncio.WindowsSelectorEventLoopPolicy()
asyncio.set_event_loop_policy(policy)
asyncio.run(main())
here, in issue 4536, everything is described in more detail.
set ssl to False when making the request
import aiohttp
conn = aiohttp.TCPConnector()
async with aiohttp.ClientSession(connector=conn) as session:
await session.get('https://example.com', ssl=False)
From docs: https://docs.aiohttp.org/en/stable/client_reference.html, params of coroutine async-with request:
ssl – SSL validation mode. None for default SSL check (ssl.create_default_context() is used), False for skip SSL certificate validation, aiohttp.Fingerprint for fingerprint validation, ssl.SSLContext for custom SSL certificate validation. Supersedes verify_ssl, ssl_context and fingerprint parameters.
New in version 3.0.
import asyncio
from aiohttp import ClientSession
async def main():
url = "https://stackoverflow.com/"
async with ClientSession() as session:
async with session.get(url, ssl=False) as resp:
print(resp.status)
asyncio.run(main())
In my case the problem was a very big amount of simultaneously opened connections. To solve the problem I have passed a limit parameter to connector:
import aiohttp
conn = aiohttp.TCPConnector(limit_per_host=5)
async with aiohttp.ClientSession(connector=conn) as session:
I just solved what could have been a 3 hour problem in 5 mins by changing all https to http. If it's possible, don't use https. I had an issue where another library (playwright) could not use the selector event loop, I would have had to separate process to use aiohttp. Better yet, switch libraries, httpx is an okay alternative.

Categories

Resources