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

(编辑:jimmy 日期: 2024/9/22 浏览:2)

软件版本及环境: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...

出错原因

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

一句话新闻

Windows上运行安卓你用过了吗
在去年的5月23日,借助Intel Bridge Technology以及Intel Celadon两项技术的驱动,Intel为PC用户带来了Android On Windows(AOW)平台,并携手国内软件公司腾讯共同推出了腾讯应用宝电脑版,将Windows与安卓两大生态进行了融合,PC的使用体验随即被带入到了一个全新的阶段。