Flask socketio split app.py: AttributeError: 'Blueprint' object has no attribute 'wsgi_app'

Flask socketio split app.py: AttributeError: 'Blueprint' object has no attribute 'wsgi_app'

我正在将 flask socketio 用于 Web 套接字项目,并使用 flask blueprint 拆分视图。问题是 socketIO 工作不正常,出现以下错误:

/flask_socketio/__init__.py", line 31, in __init__
    flask_app.wsgi_app,
AttributeError: 'Blueprint' object has no attribute 'wsgi_app'

我的项目结构如下:

|-- App
|   |-- __init__.py
|   |-- app.py
|   |-- templates
|   |   |-- main.html
|   |   |-- pymeetups.html
|   -- views
|       |-- __init__.py
|       | --- test.py
|-- __init__.py
|-- run.py

run.py:

from gevent import monkey
from flask import Flask, render_template, request
from flask_socketio import SocketIO
monkey.patch_all()

from App.views import test
app = Flask(__name__, template_folder='app/templates')
app.register_blueprint(test.app)
socketio = SocketIO(app)

if __name__ == '__main__':
    socketio.run(app, "0.0.0.0", port=80)

test.py:

from flask import Blueprint, render_template, Flask
from flask_socketio import SocketIO
app = Blueprint('app', __name__)

socketio = SocketIO(app)


@app.route('/')
def main():
    return render_template('main.html')


@socketio.on('connect', namespace='/dd')
def ws_conn():
    print("connect")
    socketio.emit('msg', {'count': 1}, namespace='/dd')


@socketio.on('disconnect', namespace="/dd")
def ws_disconn():
    print("disconnect")

问题来自 test.py 文件中的语句:

app = Blueprint('app', __name__)

socketio = SocketIO(app)

如果不使用 Flask 蓝图并将所有内容合并到一个文件中,它会完美运行:

app.py:

from gevent import monkey
from flask import Flask, render_template, request
from flask_socketio import SocketIO
monkey.patch_all()


app = Flask(__name__)
socketio = SocketIO(app)


@app.route('/')
def main():
    return render_template('main.html')



@socketio.on('connect', namespace='/dd')
def ws_conn():
    socketio.emit('msg', {'count': 1}, namespace='/dd')


@socketio.on('disconnect', namespace='/dd')
def ws_disconn():
    print("Disconnected")



if __name__ == '__main__':
    socketio.run(app, "0.0.0.0", port=80)

我如何拆分路由,因为我希望我的应用程序具有很多功能并且很难将所有功能管理到一个文件中。?

A Blueprint object works similarly to a Flask application object, but it is not actually an application.

在您的代码中,您正尝试使用蓝图注册扩展 SocketIO(blueprint)。它应该是应用程序扩展而不是蓝图扩展。像这样的东西应该适合你:

# For example my app structure like this
- app/
    __init__.py
    app.py
    main/
       __init__.py
       views.py

#app/app.py
from flask import Flask
from flask_socketio import SocketIO

app = Flask(__name__)
socketio = SocketIO(app)

from main import main

app.register_blueprint(main, url_prefix='/main')

if __name__ == '__main__':
    socketio.run(app)

#app/main/__init__.py
from flask import Blueprint

main = Blueprint('main', __name__)

from . import views

#app/main/views.py
from . import main
from app import socketio

@main.route('/')
def index():
        return "Hello"

@socketio.on('connect', namespace='/dd')
def ws_conn():
    socketio.emit('msg', {'count': 1}, namespace='/dd')

@socketio.on('disconnect', namespace='/dd')
def ws_disconn():
    print('Disconnected')