首页 > 解决方案 > `run` 属性在什么时候附加到 Bocadillo 的 App 实例?遵循教程后缺少 run()

问题描述

我按照本教程源代码,在谷歌上找不到任何关于我与 bocadillo 相关的错误的信息。罪魁祸首可能是什么?

File "/server.py", line 23, in <module>
    app.run()
AttributeError: 'App' object has no attribute 'run'

我还在调用之前记录了__dict__of并得到了这个:apprun

{
  'name': None, 'router': <bocadillo.routing.Router object at 0x[...]>,
  '_exception_middleware': <bocadillo.middleware.ExceptionMiddleware object at 0x[...]>, 
  '_asgi': <bocadillo.middleware.RequestResponseMiddleware object at 0x[...]>
}

服务器代码:

import socketio
from bocadillo import App, Templates, static

app = App()
templates = Templates(app)

# Create a socket.io async server.
# NOTE: use the asgi driver, as described in:
# https://python-socketio.readthedocs.io/en/latest/server.html#uvicorn-daphne-and-other-asgi-servers
sio = socketio.AsyncServer(async_mode="asgi")

# Create an ASGI-compliant app out of the socket.io server,
# and mount it under the root app.
# NOTE: "socket.io" is the default for `socketio_path`. We only add it
# here for the sake of being explicit.
# As a result, the client can connect at `/sio/socket.io`.
app.mount("/sio", socketio.ASGIApp(sio, socketio_path="socket.io"))

# Server static files for the socket.io client.
# See: https://github.com/socketio/socket.io-client
# NOTE: alternatively, the socket.io client could be served from
# a CDN if you don't have npm/Node.js available in your runtime.
# If so, static files would be linked to in the HTML page, and we wouldn't
# need this line.
# See: https://socket.io/docs/#Javascript-Client
app.mount("/socket.io", static("node_modules/socket.io-client/dist"))


@app.route("/")
async def index(req, res):
    res.html = await templates.render("index.html")


# Event handler for the "message" event.
# See: https://python-socketio.readthedocs.io/en/latest/server.html#defining-event-handlers
@sio.on("message")
async def handle_message(sid, data: str):
    print("message:", data)
    # Broadcast the received message to all connected clients.
    # See: https://python-socketio.readthedocs.io/en/latest/server.html#emitting-events
    await sio.emit("response", data)


if __name__ == "__main__":
    app.run()

我的 Python 版本是 3.8.2,超过了教程的要求。我之前添加了一些调试代码app.run()来弄清楚到底发生了什么:

...
if __name__ == "__main__":

    for key in app.__dict__.keys():
        item = getattr(app, key)
        #view the type of each attribute
        print(key, "type:", type(item))
        if hasattr(item, "__dict__"):
            #if it has a __dict__ attribute, get its vars() output
            print(key, ":", vars(item))
        else:
            # else print it plainly
            print(key, ":", item)

奇怪的是,即使类型是某种形式,class根据这个问题的答案,所有这些对象都应该有一个__dict__if hasattr(item, "__dict__"):检查失败并且每个项目都清楚地打印出来。所以我不知道如何查看对象的内容。

输出:

name type: <class 'NoneType'>
name: None
router type: <class 'bocadillo.routing.Router'>
router: <bocadillo.routing.Router object at 0x[...]>
_exception_middleware type: <class 'bocadillo.middleware.ExceptionMiddleware'>
_exception_middleware : <bocadillo.middleware.ExceptionMiddleware object at 0x[...]>
_asgi type: <class 'bocadillo.middleware.RequestResponseMiddleware'>
_asgi : <bocadillo.middleware.RequestResponseMiddleware object at 0x[...]>

我不知道发生了什么,也不知道还有什么可以尝试的。在这一点上,作为一个新的/正在学习 Python 开发人员,我发现自己试图通过源代码来找出可能导致run丢失的原因,而且在语言经验如此少的情况下,这并不是一个富有成果的冒险,

标签: pythonasgi

解决方案


推荐阅读