ERROR:aiohttp.web:Error handling request - python-3.x

i want to implement account register in my web. everything is running ok but one error, i can not insert account information in my aiomysql. there is the traceback:
INFO:root:Request: POST /api/users
INFO:root:check user: POST /api/users
INFO:root:Response handler...
INFO:root:call with args: {'passwd': '1a4eb93dd425112e9b64374172fede31d85c462d', 'email': 'mike#163.com', 'name': 'mike'}
INFO:root:SQL: select `id`, `created_at`, `image`, `admin`, `passwd`, `name`, `email` from `users` where email=?
INFO:root:rows returned: 0
INFO:root:SQL: insert into `users` (`created_at`, `image`, `admin`, `passwd`, `name`, `email`, `id`) values (?, ?, ?, ?, ?, ?, ?)
ERROR:aiohttp.web:Error handling request
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/aiohttp/server.py", line 266, in start
yield from self.handle_request(message, payload)
File "/usr/lib/python3/dist-packages/aiohttp/web.py", line 87, in handle_request
resp = yield from handler(request)
File "/home/carrot/hqinawesomewebapp/www/app.py", line 46, in logger
return (await handler(request))
File "/home/carrot/hqinawesomewebapp/www/app.py", line 61, in auth
return (await handler(request))
File "/home/carrot/hqinawesomewebapp/www/app.py", line 80, in response
r = await handler(request)
File "/usr/lib/python3.5/asyncio/coroutines.py", line 219, in coro
res = yield from await_meth()
File "/home/carrot/hqinawesomewebapp/www/coroweb.py", line 136, in __call__
r = await self._func(**kw)
File "/usr/lib/python3.5/asyncio/coroutines.py", line 219, in coro
res = yield from await_meth()
File "/home/carrot/hqinawesomewebapp/www/handlers.py", line 174, in api_register_user
await user.save()
File "/home/carrot/hqinawesomewebapp/www/orm.py", line 218, in save
rows = await execute(self.__insert__, args)
File "/home/carrot/hqinawesomewebapp/www/orm.py", line 53, in execute
await cur.execute(sql.replace('?', '%s'), args)
File "/usr/local/lib/python3.5/dist-packages/aiomysql/cursors.py", line 237, in execute
query = query % self._escape_args(args, conn)
File "/usr/local/lib/python3.5/dist-packages/aiomysql/cursors.py", line 196, in _escape_args
return tuple(conn.escape(arg) for arg in args)
File "/usr/local/lib/python3.5/dist-packages/aiomysql/cursors.py", line 196, in <genexpr>
return tuple(conn.escape(arg) for arg in args)
File "/usr/local/lib/python3.5/dist-packages/aiomysql/connection.py", line 356, in escape
return escape_item(obj, self._charset)
File "/usr/local/lib/python3.5/dist-packages/pymysql/converters.py", line 27, in escape_item
val = encoder(val, mapping)
File "/usr/local/lib/python3.5/dist-packages/pymysql/converters.py", line 110, in escape_unicode
return u"'%s'" % _escape_unicode(value)
File "/usr/local/lib/python3.5/dist-packages/pymysql/converters.py", line 73, in _escape_unicode
return value.translate(_escape_table)
AttributeError: 'builtin_function_or_method' object has no attribute 'translate'
in my case, i can do select operation, so my orm.py is ok. but when insert a new account , the page will occur this:
"There is something wrong with Internet as 500 (HTTP) "
how can fix it? thx :-)

Most likely you a passing a function (func) into SQL call as argument instead of function call result (func()).

in my middlewares has a function named auth_factory to verify the user:
async def auth_factory(app, handler):
async def auth(request):
logging.info('check user: %s %s' % (request.method, request.path))
request.__user__ = None
cookie_str = request.cookies.get(COOKIE_NAME)
if cookie_str:
user = await cookie2user(cookie_str)
if user:
# i wrong here
logging.info('set current user: %s' % user.email)
...
i got an error in user.email such as user.eamil
then everything is ok
why this middlerwares function impact so big ?
i even can not insert data into mysql, these two thing has some relationship?

Related

sqlalchemy + postgres asyncpg.exceptions.ForeignKeyViolationError on relationship()

So far in my project I was prototyping and used sqlite with sqlalchemy.
I have a couple of tables with oneToMany relationships, all works nicely.
Now, after switching to postgres, I get the asyncpg.exceptions.ForeignKeyViolationError for the tables where I defined ForeignKeys.
Here is my sqlalchemy parent and child model:
from sqlalchemy.orm import relationship
from .database import Base
from fastapi_users_db_sqlalchemy import SQLAlchemyBaseUserTable
class Company(Base):
__tablename__ = "company"
id = Column(Integer, primary_key=True, index=True)
name = Column(String, unique=True)
type = Column(
String(length=10),
server_default="No type given",
nullable=False,
)
users = relationship("User", back_populates="company")
class User(Base, SQLAlchemyBaseUserTable):
first_name = Column(
String(length=50),
index=True,
server_default="No name given",
nullable=False,
)
company_id = Column(Integer, ForeignKey("company.id"), nullable=False)
company = relationship("Company", back_populates="users")
When trying to register a user, I get:
INFO: 172.17.0.1:63534 - "POST /auth/register HTTP/1.1" 500 Internal Server Error
ERROR: Exception in ASGI application
Traceback (most recent call last):
File "/usr/lib/python3.9/site-packages/uvicorn/protocols/http/h11_impl.py", line 373, in run_asgi
result = await app(self.scope, self.receive, self.send)
File "/usr/lib/python3.9/site-packages/uvicorn/middleware/proxy_headers.py", line 75, in __call__
return await self.app(scope, receive, send)
File "/usr/lib/python3.9/site-packages/fastapi/applications.py", line 208, in __call__
await super().__call__(scope, receive, send)
File "/usr/lib/python3.9/site-packages/starlette/applications.py", line 112, in __call__
await self.middleware_stack(scope, receive, send)
File "/usr/lib/python3.9/site-packages/starlette/middleware/errors.py", line 181, in __call__
raise exc
File "/usr/lib/python3.9/site-packages/starlette/middleware/errors.py", line 159, in __call__
await self.app(scope, receive, _send)
File "/usr/lib/python3.9/site-packages/starlette/middleware/cors.py", line 92, in __call__
await self.simple_response(scope, receive, send, request_headers=headers)
File "/usr/lib/python3.9/site-packages/starlette/middleware/cors.py", line 147, in simple_response
await self.app(scope, receive, send)
File "/usr/lib/python3.9/site-packages/starlette/exceptions.py", line 82, in __call__
raise exc
File "/usr/lib/python3.9/site-packages/starlette/exceptions.py", line 71, in __call__
await self.app(scope, receive, sender)
File "/usr/lib/python3.9/site-packages/starlette/routing.py", line 656, in __call__
await route.handle(scope, receive, send)
File "/usr/lib/python3.9/site-packages/starlette/routing.py", line 259, in handle
await self.app(scope, receive, send)
File "/usr/lib/python3.9/site-packages/starlette/routing.py", line 61, in app
response = await func(request)
File "/usr/lib/python3.9/site-packages/fastapi/routing.py", line 226, in app
raw_response = await run_endpoint_function(
File "/usr/lib/python3.9/site-packages/fastapi/routing.py", line 159, in run_endpoint_function
return await dependant.call(**values)
File "/usr/lib/python3.9/site-packages/fastapi_users/router/register.py", line 32, in register
created_user = await user_manager.create(user, safe=True, request=request)
File "/usr/lib/python3.9/site-packages/fastapi_users/manager.py", line 153, in create
created_user = await self.user_db.create(db_user)
File "/usr/lib/python3.9/site-packages/fastapi_users_db_sqlalchemy/__init__.py", line 159, in create
await self.database.execute(query, user_dict)
File "/usr/lib/python3.9/site-packages/databases/core.py", line 169, in execute
return await connection.execute(query, values)
File "/usr/lib/python3.9/site-packages/databases/core.py", line 295, in execute
return await self._connection.execute(built_query)
File "/usr/lib/python3.9/site-packages/databases/backends/postgres.py", line 210, in execute
return await self._connection.fetchval(query_str, *args)
File "/usr/lib/python3.9/site-packages/asyncpg/connection.py", line 645, in fetchval
data = await self._execute(query, args, 1, timeout)
File "/usr/lib/python3.9/site-packages/asyncpg/connection.py", line 1659, in _execute
result, _ = await self.__execute(
File "/usr/lib/python3.9/site-packages/asyncpg/connection.py", line 1684, in __execute
return await self._do_execute(
File "/usr/lib/python3.9/site-packages/asyncpg/connection.py", line 1731, in _do_execute
result = await executor(stmt, None)
File "asyncpg/protocol/protocol.pyx", line 201, in bind_execute
asyncpg.exceptions.ForeignKeyViolationError: insert or update on table "user" violates foreign key constraint "user_company_id_fkey"
DETAIL: Key (company_id)=(1) is not present in table "company".
The Error is right, there is no column company_id in table company - only id.
I defined to use the value company.id for column company_id in the user-table.
Why is the program checking on company_id?
I followed this sqlalchemy documentation to define the relationships: https://docs.sqlalchemy.org/en/14/orm/basic_relationships.html
It works fine like this with sqlite.
I can see in the traceback, that it connects to the right backend (postgres), so does that mean, sqlalchemy is not translating the relationship in the right way for postgres?
Any ideas how to solve this?
Solved - for anyone who has this problem as well:
postgres does not seem to reset the id-counter like sqlite does. Since I created test data in a script, always starting from 1 up to the range limit, I used ids that did not exist anymore.
To put it into my example: The company_id I used to create a new user plainly did not exist anymore.
This, I discovered by accident. The error message misled me completely.

Telegram bot aiogram erros

mistakes
mistakes 2
photo
https://github.com/mahenzon/aiogram-lessons/tree/master/lesson-02
I can't understand why my code isn't working. I took code from here and only change file config.py, change token and MY_ID.
executor.py [ LINE:362 ]# INFO [2021-08-12 14:43:46,938] Bot: lap156 [#lab156_bot]
dispatcher.py [ LINE:360 ]# INFO [2021-08-12 14:43:46,938] Start polling.
base_events.py [ LINE:1738 ]# ERROR [2021-08-12 14:47:59,585] Task exception was never retrieved
future: <Task finished name='Task-24' coro=<Dispatcher._process_polling_updates() done, defined at C:\Users\Zver\AppData\Local\Programs\Python\Python39\lib\site-packages\aiogram\dispatcher\dispatcher.py:409> exception=WrongFileIdentifier('Wrong file identifier/http url specified')>
Traceback (most recent call last):
File "C:\Users\Zver\AppData\Local\Programs\Python\Python39\lib\site-packages\aiogram\dispatcher\dispatcher.py", line 417, in _process_polling_updates
for responses in itertools.chain.from_iterable(await self.process_updates(updates, fast)):
File "C:\Users\Zver\AppData\Local\Programs\Python\Python39\lib\site-packages\aiogram\dispatcher\dispatcher.py", line 238, in process_updates
return await asyncio.gather(*tasks)
File "C:\Users\Zver\AppData\Local\Programs\Python\Python39\lib\site-packages\aiogram\dispatcher\handler.py", line 116, in notify
response = await handler_obj.handler(*args, **partial_data)
File "C:\Users\Zver\AppData\Local\Programs\Python\Python39\lib\site-packages\aiogram\dispatcher\dispatcher.py", line 259, in process_update
return await self.message_handlers.notify(update.message)
File "C:\Users\Zver\AppData\Local\Programs\Python\Python39\lib\site-packages\aiogram\dispatcher\handler.py", line 116, in notify
response = await handler_obj.handler(*args, **partial_data)
File "C:\Users\Zver\aiogram-lessons\lesson-02\bot.py", line 55, in process_photo_command
await bot.send_photo(message.from_user.id, CAT_BIG_EYES,
File "C:\Users\Zver\AppData\Local\Programs\Python\Python39\lib\site-packages\aiogram\bot\bot.py", line 482, in send_photo
result = await self.request(api.Methods.SEND_PHOTO, payload, files)
File "C:\Users\Zver\AppData\Local\Programs\Python\Python39\lib\site-packages\aiogram\bot\base.py", line 208, in request
return await api.make_request(self.session, self.server, self.__token, method, data, files,
File "C:\Users\Zver\AppData\Local\Programs\Python\Python39\lib\site-packages\aiogram\bot\api.py", line 140, in make_request
return check_result(method, response.content_type, response.status, await response.text())
File "C:\Users\Zver\AppData\Local\Programs\Python\Python39\lib\site-packages\aiogram\bot\api.py", line 115, in check_result
exceptions.BadRequest.detect(description)
File "C:\Users\Zver\AppData\Local\Programs\Python\Python39\lib\site-packages\aiogram\utils\exceptions.py", line 140, in detect
raise err(cls.text or description)
aiogram.utils.exceptions.WrongFileIdentifier: Wrong file identifier/http url specified
base_events.py [ LINE:1738 ]# ERROR [2021-08-12 14:48:03,624] Task exception was never retrieved
future: <Task finished name='Task-32' coro=<Dispatcher._process_polling_updates() done, defined at C:\Users\Zver\AppData\Local\Programs\Python\Python39\lib\site-packages\aiogram\dispatcher\dispatcher.py:409> exception=WrongFileIdentifier('Wrong file identifier/http url specified')>
Traceback (most recent call last):
File "C:\Users\Zver\AppData\Local\Programs\Python\Python39\lib\site-packages\aiogram\dispatcher\dispatcher.py", line 417, in _process_polling_updates
for responses in itertools.chain.from_iterable(await self.process_updates(updates, fast)):
File "C:\Users\Zver\AppData\Local\Programs\Python\Python39\lib\site-packages\aiogram\dispatcher\dispatcher.py", line 238, in process_updates
return await asyncio.gather(*tasks)
File "C:\Users\Zver\AppData\Local\Programs\Python\Python39\lib\site-packages\aiogram\dispatcher\handler.py", line 116, in notify
response = await handler_obj.handler(*args, **partial_data)
File "C:\Users\Zver\AppData\Local\Programs\Python\Python39\lib\site-packages\aiogram\dispatcher\dispatcher.py", line 259, in process_update
return await self.message_handlers.notify(update.message)
File "C:\Users\Zver\AppData\Local\Programs\Python\Python39\lib\site-packages\aiogram\dispatcher\handler.py", line 116, in notify
response = await handler_obj.handler(*args, **partial_data)
File "C:\Users\Zver\aiogram-lessons\lesson-02\bot.py", line 73, in process_note_command
await bot.send_video_note(message.from_user.id, VIDEO_NOTE)
File "C:\Users\Zver\AppData\Local\Programs\Python\Python39\lib\site-packages\aiogram\bot\bot.py", line 963, in send_video_note
result = await self.request(api.Methods.SEND_VIDEO_NOTE, payload, files)
File "C:\Users\Zver\AppData\Local\Programs\Python\Python39\lib\site-packages\aiogram\bot\base.py", line 208, in request
return await api.make_request(self.session, self.server, self.__token, method, data, files,
File "C:\Users\Zver\AppData\Local\Programs\Python\Python39\lib\site-packages\aiogram\bot\api.py", line 140, in make_request
return check_result(method, response.content_type, response.status, await response.text())
File "C:\Users\Zver\AppData\Local\Programs\Python\Python39\lib\site-packages\aiogram\bot\api.py", line 115, in check_result
exceptions.BadRequest.detect(description)
File "C:\Users\Zver\AppData\Local\Programs\Python\Python39\lib\site-packages\aiogram\utils\exceptions.py", line 140, in detect
raise err(cls.text or description)
aiogram.utils.exceptions.WrongFileIdentifier: Wrong file identifier/http url specified
base_events.py [ LINE:1738 ]# ERROR [2021-08-12 14:48:06,943] Task exception was never retrieved
future: <Task finished name='Task-37' coro=<Dispatcher._process_polling_updates() done, defined at C:\Users\Zver\AppData\Local\Programs\Python\Python39\lib\site-packages\aiogram\dispatcher\dispatcher.py:409> exception=WrongFileIdentifier('Wrong file identifier/http url specified')>
Traceback (most recent call last):
File "C:\Users\Zver\AppData\Local\Programs\Python\Python39\lib\site-packages\aiogram\dispatcher\dispatcher.py", line 417, in _process_polling_updates
for responses in itertools.chain.from_iterable(await self.process_updates(updates, fast)):
File "C:\Users\Zver\AppData\Local\Programs\Python\Python39\lib\site-packages\aiogram\dispatcher\dispatcher.py", line 238, in process_updates
return await asyncio.gather(*tasks)
File "C:\Users\Zver\AppData\Local\Programs\Python\Python39\lib\site-packages\aiogram\dispatcher\handler.py", line 116, in notify
response = await handler_obj.handler(*args, **partial_data)
File "C:\Users\Zver\AppData\Local\Programs\Python\Python39\lib\site-packages\aiogram\dispatcher\dispatcher.py", line 259, in process_update
return await self.message_handlers.notify(update.message)
File "C:\Users\Zver\AppData\Local\Programs\Python\Python39\lib\site-packages\aiogram\dispatcher\handler.py", line 116, in notify
response = await handler_obj.handler(*args, **partial_data)
File "C:\Users\Zver\aiogram-lessons\lesson-02\bot.py", line 55, in process_photo_command
await bot.send_photo(message.from_user.id, CAT_BIG_EYES,
File "C:\Users\Zver\AppData\Local\Programs\Python\Python39\lib\site-packages\aiogram\bot\bot.py", line 482, in send_photo
result = await self.request(api.Methods.SEND_PHOTO, payload, files)
File "C:\Users\Zver\AppData\Local\Programs\Python\Python39\lib\site-packages\aiogram\bot\base.py", line 208, in request
return await api.make_request(self.session, self.server, self.__token, method, data, files,
File "C:\Users\Zver\AppData\Local\Programs\Python\Python39\lib\site-packages\aiogram\bot\api.py", line 140, in make_request
return check_result(method, response.content_type, response.status, await response.text())
File "C:\Users\Zver\AppData\Local\Programs\Python\Python39\lib\site-packages\aiogram\bot\api.py", line 115, in check_result
exceptions.BadRequest.detect(description)
File "C:\Users\Zver\AppData\Local\Programs\Python\Python39\lib\site-packages\aiogram\utils\exceptions.py", line 140, in detect
raise err(cls.text or description)
aiogram.utils.exceptions.WrongFileIdentifier: Wrong file identifier/http url specified
base_events.py [ LINE:1738 ]# ERROR [2021-08-12 14:48:15,298] Task exception was never retrieved
future: <Task finished name='Task-41' coro=<Dispatcher._process_polling_updates() done, defined at C:\Users\Zver\AppData\Local\Programs\Python\Python39\lib\site-packages\aiogram\dispatcher\dispatcher.py:409> exception=WrongFileIdentifier('Wrong file identifier/http url specified')>
Traceback (most recent call last):
File "C:\Users\Zver\AppData\Local\Programs\Python\Python39\lib\site-packages\aiogram\dispatcher\dispatcher.py", line 417, in _process_polling_updates
for responses in itertools.chain.from_iterable(await self.process_updates(updates, fast)):
File "C:\Users\Zver\AppData\Local\Programs\Python\Python39\lib\site-packages\aiogram\dispatcher\dispatcher.py", line 238, in process_updates
return await asyncio.gather(*tasks)
File "C:\Users\Zver\AppData\Local\Programs\Python\Python39\lib\site-packages\aiogram\dispatcher\handler.py", line 116, in notify
response = await handler_obj.handler(*args, **partial_data)
File "C:\Users\Zver\AppData\Local\Programs\Python\Python39\lib\site-packages\aiogram\dispatcher\dispatcher.py", line 259, in process_update
return await self.message_handlers.notify(update.message)
File "C:\Users\Zver\AppData\Local\Programs\Python\Python39\lib\site-packages\aiogram\dispatcher\handler.py", line 116, in notify
response = await handler_obj.handler(*args, **partial_data)
File "C:\Users\Zver\aiogram-lessons\lesson-02\bot.py", line 73, in process_note_command
await bot.send_video_note(message.from_user.id, VIDEO_NOTE)
File "C:\Users\Zver\AppData\Local\Programs\Python\Python39\lib\site-packages\aiogram\bot\bot.py", line 963, in send_video_note
result = await self.request(api.Methods.SEND_VIDEO_NOTE, payload, files)
File "C:\Users\Zver\AppData\Local\Programs\Python\Python39\lib\site-packages\aiogram\bot\base.py", line 208, in request
return await api.make_request(self.session, self.server, self.__token, method, data, files,
File "C:\Users\Zver\AppData\Local\Programs\Python\Python39\lib\site-packages\aiogram\bot\api.py", line 140, in make_request
return check_result(method, response.content_type, response.status, await response.text())
File "C:\Users\Zver\AppData\Local\Programs\Python\Python39\lib\site-packages\aiogram\bot\api.py", line 115, in check_result
exceptions.BadRequest.detect(description)
File "C:\Users\Zver\AppData\Local\Programs\Python\Python39\lib\site-packages\aiogram\utils\exceptions.py", line 140, in detect
raise err(cls.text or description)
aiogram.utils.exceptions.WrongFileIdentifier: Wrong file identifier/http url specified
I guess the error it seems that one running terminal of the bot is already running when you run your code. So Terminate all running terminals/processes and try again.

FastAPI Raises Missing Positional Argument Error

The following code raises the following exception.
I'm not sure I fully understand how FastAPI works. Could someone help me understand?
from fastapi import FastAPI
from fastapi import BackgroundTasks
from fastapi import Request
app = FastAPI()
async def parse_request(req: Request):
print(req)
print(req.client.host)
#app.route("/endpoint")
async def endpoint(request: Request, background_tasks: BackgroundTasks):
background_tasks.add_task(parse_request, request)
return {"msg": "ok"}
Exception:
Traceback (most recent call last):
File "/home/user/repos/fastapi_pg/env_39/lib/python3.9/site-packages/uvicorn/protocols/http/h11_impl.py", line 396, in run_asgi
result = await app(self.scope, self.receive, self.send)
File "/home/user/repos/fastapi_pg/env_39/lib/python3.9/site-packages/uvicorn/middleware/proxy_headers.py", line 45, in __call__
return await self.app(scope, receive, send)
File "/home/user/repos/fastapi_pg/env_39/lib/python3.9/site-packages/fastapi/applications.py", line 199, in __call__
await super().__call__(scope, receive, send)
File "/home/user/repos/fastapi_pg/env_39/lib/python3.9/site-packages/starlette/applications.py", line 112, in __call__
await self.middleware_stack(scope, receive, send)
File "/home/user/repos/fastapi_pg/env_39/lib/python3.9/site-packages/starlette/middleware/errors.py", line 181, in __call__
raise exc from None
File "/home/user/repos/fastapi_pg/env_39/lib/python3.9/site-packages/starlette/middleware/errors.py", line 159, in __call__
await self.app(scope, receive, _send)
File "/home/user/repos/fastapi_pg/env_39/lib/python3.9/site-packages/starlette/exceptions.py", line 82, in __call__
raise exc from None
File "/home/user/repos/fastapi_pg/env_39/lib/python3.9/site-packages/starlette/exceptions.py", line 71, in __call__
await self.app(scope, receive, sender)
File "/home/user/repos/fastapi_pg/env_39/lib/python3.9/site-packages/starlette/routing.py", line 580, in __call__
await route.handle(scope, receive, send)
File "/home/user/repos/fastapi_pg/env_39/lib/python3.9/site-packages/starlette/routing.py", line 241, in handle
await self.app(scope, receive, send)
File "/home/user/repos/fastapi_pg/env_39/lib/python3.9/site-packages/starlette/routing.py", line 52, in app
response = await func(request)
TypeError: endpoint() missing 1 required positional argument: 'background_tasks'
I've gotten this to work without the Request param when I pass in my own variable in the route path:
#app.get("/test/{input_str}")
async def test(input_str: str, background_tasks: BackgroundTasks):
background_tasks.add_task(do_stuff, input_str)
return {"Hello": "World"}
Seems like Starlette doesn't know how to handle multiple of these params being passed so you can't mix and match. Ideal I'd be able to take the Request and Header objects and pass them to a background task for processing.
The decorator #app.route seems to be the problem. (see: https://github.com/tiangolo/fastapi/issues/912) Use #app.api_route instead or even better #app.get or #app.post.

Async in aiogram python. Pull out from the async function

I wrote a program on python using aiogram, now I'm refactoring the code. There was a problem that I cannot take out a piece of code from do_something func to the get_task_info func:
async def get_task_info(task_info, message: types.Message):
if message.content_type == 'text':
task_info.append(message.text)
elif message.content_type == 'photo':
task_info.extend([str(message.caption),
await message.photo[-1].get_url()])
elif message.content_type == 'document':
task_info.extend([str(message.caption),
await message.document.get_url()])
return task_info
#dp.message_handler(state=Order.some_state)
async def do_something(message: types.Message, state: FSMContext):
data = await state.get_data()
task_info = data.get('task_info', list())
task_info = get_task_info(task_info, message)
await state.update_data(task_info=task_info)
await message.answer('Done')
And when I try so there is an exception:
ERROR:asyncio:Task exception was never retrieved
future: <Task finished coro=<Dispatcher._process_polling_updates() done, defined at C:\Users\HP\miniconda3\lib\site-packages\aiogram\dispatcher\dispatcher.py:331> exception=TypeError("can't pickle coroutine objects")>
Traceback (most recent call last):
File "C:\Users\HP\miniconda3\lib\site-packages\aiogram\dispatcher\dispatcher.py", line 339, in _process_polling_updates
for responses in itertools.chain.from_iterable(await self.process_updates(updates, fast)):
File "C:\Users\HP\miniconda3\lib\site-packages\aiogram\dispatcher\dispatcher.py", line 194, in process_updates
return await asyncio.gather(*tasks)
File "C:\Users\HP\miniconda3\lib\site-packages\aiogram\dispatcher\handler.py", line 117, in notify
response = await handler_obj.handler(*args, **partial_data)
File "C:\Users\HP\miniconda3\lib\site-packages\aiogram\dispatcher\dispatcher.py", line 214, in process_update
return await self.message_handlers.notify(update.message)
File "C:\Users\HP\miniconda3\lib\site-packages\aiogram\dispatcher\handler.py", line 117, in notify
response = await handler_obj.handler(*args, **partial_data)
File "F:\Telegram\handlers\order.py", line 110, in commit_task
data = await state.get_data()
File "C:\Users\HP\miniconda3\lib\site-packages\aiogram\dispatcher\storage.py", line 298, in get_data
return await self.storage.get_data(chat=self.chat, user=self.user, default=default)
File "C:\Users\HP\miniconda3\lib\site-packages\aiogram\contrib\fsm_storage\memory.py", line 45, in get_data
return copy.deepcopy(self.data[chat][user]['data'])
File "C:\Users\HP\miniconda3\lib\copy.py", line 150, in deepcopy
y = copier(x, memo)
File "C:\Users\HP\miniconda3\lib\copy.py", line 240, in _deepcopy_dict
y[deepcopy(key, memo)] = deepcopy(value, memo)
File "C:\Users\HP\miniconda3\lib\copy.py", line 169, in deepcopy
rv = reductor(4)
TypeError: can't pickle coroutine objects
Process finished with exit code -1
What should I do?
get_task_info is the async function, you should do async call for getting result adding await operator.
replace
task_info = get_task_info(task_info, message)
to
task_info = await get_task_info(task_info, message)

Proxybroker - AttributeError 'dict' object has no attribute 'expired'

Well I have this weird problem. Same script works on windows7 and on windows10 throws AttributeError. And I just can't figure out what is the difference.
This is py code:
import asyncio
from proxybroker import Broker
async def use(proxies):
while True:
proxy = await proxies.get()
if proxy is None:
break
elif 'SOCKS5' in proxy.types: # filter by type
print('Found SOCKS5 proxy: %s' % proxy)
else:
print('Found proxy: %s' % proxy)
async def find(proxies, loop):
broker = Broker(queue=proxies,
timeout=8,
attempts_conn=3,
max_concurrent_conn=200,
judges=['https://httpheader.net/', 'http://httpheader.net/'],
providers=['http://www.proxylists.net/', 'http://fineproxy.org/eng/'],
verify_ssl=False,
loop=loop)
# only anonymous & high levels of anonymity for http protocol and high for others:
types = [('HTTP', ('Anonymous', 'High')), 'HTTPS', 'SOCKS4', 'SOCKS5']
countries = ['US', 'GB', 'DE']
limit = 10
await broker.find(types=types, countries=countries, limit=limit)
if __name__ == '__main__':
loop = asyncio.get_event_loop()
proxies = asyncio.Queue(loop=loop)
tasks = asyncio.gather(find(proxies, loop), use(proxies))
loop.run_until_complete(tasks)
And this is the error that it throws:
C:\Users\draga\AppData\Local\Programs\Python\Python36-32\python.exe "C:/Users/draga/Desktop/Rts/PROXY/Proxy.py"
C:\Users\draga\AppData\Local\Programs\Python\Python36-32\lib\site-packages\aiohttp\client.py:576: DeprecationWarning: Use async with instead
warnings.warn("Use async with instead", DeprecationWarning)
C:\Users\draga\AppData\Local\Programs\Python\Python36-32\lib\site-packages\aiohttp\helpers.py:72: DeprecationWarning: ClientSession.close() is not coroutine
warnings.warn(self._msg, DeprecationWarning)
C:\Users\draga\AppData\Local\Programs\Python\Python36-32\lib\site-packages\aiohttp\helpers.py:72: DeprecationWarning: ClientSession.close() is not coroutine
warnings.warn(self._msg, DeprecationWarning)
C:\Users\draga\AppData\Local\Programs\Python\Python36-32\lib\site-packages\aiohttp\helpers.py:72: DeprecationWarning: ClientSession.close() is not coroutine
warnings.warn(self._msg, DeprecationWarning)
Traceback (most recent call last):
File "C:/Users/draga/Desktop/Rts/PROXY/Proxy.py", line 35, in <module>
loop.run_until_complete(tasks)
File "C:\Users\draga\AppData\Local\Programs\Python\Python36-32\lib\asyncio\base_events.py", line 466, in run_until_complete
return future.result()
File "C:/Users/draga/Desktop/Rts/PROXY/Proxy.py", line 29, in find
await broker.find(types=types, countries=countries, limit=limit)
File "C:\Users\draga\AppData\Local\Programs\Python\Python36-32\lib\site-packages\proxybroker\api.py", line 108, in find
await self._run(self._checker.check_judges(), action)
File "C:\Users\draga\AppData\Local\Programs\Python\Python36-32\lib\site-packages\proxybroker\api.py", line 114, in _run
await tasks
File "C:\Users\draga\AppData\Local\Programs\Python\Python36-32\lib\site-packages\proxybroker\api.py", line 153, in _grab
proxies = await task
File "C:\Users\draga\AppData\Local\Programs\Python\Python36-32\lib\asyncio\tasks.py", line 452, in _wait_for_one
return f.result() # May raise f.exception().
File "C:\Users\draga\AppData\Local\Programs\Python\Python36-32\lib\site-packages\proxybroker\providers.py", line 68, in get_proxies
await self._pipe()
File "C:\Users\draga\AppData\Local\Programs\Python\Python36-32\lib\site-packages\proxybroker\providers.py", line 101, in _pipe
await self._find_on_page(self.url)
File "C:\Users\draga\AppData\Local\Programs\Python\Python36-32\lib\site-packages\proxybroker\providers.py", line 117, in _find_on_page
page = await self.get(url, data=data, headers=headers, method=method)
File "C:\Users\draga\AppData\Local\Programs\Python\Python36-32\lib\site-packages\proxybroker\providers.py", line 132, in get
page = await self._get(url, data=data, headers=headers, method=method)
File "C:\Users\draga\AppData\Local\Programs\Python\Python36-32\lib\site-packages\proxybroker\providers.py", line 144, in _get
method, url, data=data, headers=headers) as resp:
File "C:\Users\draga\AppData\Local\Programs\Python\Python36-32\lib\site-packages\aiohttp\client.py", line 637, in __aenter__
self._resp = yield from self._coro
File "C:\Users\draga\AppData\Local\Programs\Python\Python36-32\lib\site-packages\aiohttp\client.py", line 231, in _request
conn = yield from self._connector.connect(req)
File "C:\Users\draga\AppData\Local\Programs\Python\Python36-32\lib\site-packages\aiohttp\connector.py", line 378, in connect
proto = yield from self._create_connection(req)
File "C:\Users\draga\AppData\Local\Programs\Python\Python36-32\lib\site-packages\aiohttp\connector.py", line 686, in _create_connection
_, proto = yield from self._create_direct_connection(req)
File "C:\Users\draga\AppData\Local\Programs\Python\Python36-32\lib\site-packages\aiohttp\connector.py", line 697, in _create_direct_connection
hosts = yield from self._resolve_host(req.url.raw_host, req.port)
File "C:\Users\draga\AppData\Local\Programs\Python\Python36-32\lib\site-packages\aiohttp\connector.py", line 666, in _resolve_host
self._cached_hosts.expired(key):
AttributeError: 'dict' object has no attribute 'expired'
Update: it seems the problem is in loop.run_until_complete(tasks) still dunno why.
ProxyBroker doesn't works after updating aiohttp to 2.1.0... You have to downgrade it to older version.

Resources