Python中使用aiohttp模拟服务器出现错误问题及解决办法

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Python中使用aiohttp模拟服务器出现错误问题及解决办法

ば你   2020-10-31 我要评论

软件版本及环境:Python 3.9 + pycharm 2020.2.1 + Windows10 运行报错:

  • DeprecationWarning: loop argument is deprecated
  • app = web.Application(loop=loop)
  • DeprecationWarning: Application.make_handler(…) is deprecated, use AppRunner API instead
  • srv = await loop.create_server(app.make_handler(), ‘127.0.0.1', 8000)

出错代码

async def init(loop):
 app = web.Application(loop=loop)
 app.router.add_route('GET', '/', index)
 app.router.add_route('GET', '/hello/{name}', hello)
 srv = await loop.create_server(app.make_handler(), '127.0.0.1', 8000)
 print("Server started at http://127.0.0.1:8000...")
 return srv

解决方法 删除loop=loop

app = web.Application()

将app.make_handler()改为app()

srv = await loop.create_server(app(), '127.0.0.1', 8000)

运行结果

Server started at http://127.0.0.1:8000...

出错原因

新版本改动了库函数的使用

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们