1. 程式人生 > 程式設計 >Python中使用aiohttp模擬伺服器出現錯誤問題及解決方法

Python中使用aiohttp模擬伺服器出現錯誤問題及解決方法

軟體版本及環境: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(),8000)

執行結果

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

出錯原因

新版本改動了庫函式的使用

到此這篇關於Python中使用aiohttp模擬伺服器出現錯誤的文章就介紹到這了,更多相關Python中使用aiohttp模擬伺服器出現錯誤內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!