Flask socketio split app.py:AttributeError:' Blueprint'对象没有属性' wsgi_app'

时间:2017-01-14 18:13:32

标签: python-3.x flask flask-socketio

我正在使用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)

如果不使用烧瓶蓝图并将所有组合成一个单独的文件,它可以完美地运行:

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)

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

1 个答案:

答案 0 :(得分:3)

  

Blueprint对象与Flask应用程序对象的工作方式类似,但是   它实际上不是一个应用程序。

在您的代码中,您尝试使用蓝图注册扩展程序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')