WSGI中间件和应用工厂

时间:2012-11-24 04:05:33

标签: python-2.7 flask mod-wsgi wsgi middleware

我正在尝试在我的服务器上为python设置中间件,以便服务器上的apache2可以看到蓝图的子目录,并在 example.com/myapplicationsubfolder/routedblueprintfunction

我的 __ init __。py 在/ users /文件夹中如下所示:

class WebFactionMiddleware(object):
    def __init__(self, app):
        self.app = app
    def __call__(self, environ, start_response):
        environ['SCRIPT_NAME'] = '/myapplicationsubfolder'
        return self.app(environ, start_response)


from index import application
application.wsgi_app = WebFactionMiddleware(application.wsgi_app)

在我制作应用程序工厂之后,我再也无法使用它了:

from app import app
app.wsgi_app = WebFactionMiddleware(app.wsgi_app)

我正试图从index.py

获取烧瓶应用程序

Index.py 是这样的:

from app import create_app
application = create_app()
application.run()
像这样

app.py

def configure_errors(app):
  @app.errorhandler(500)
  def internal_server_error(error):
      return render_template('500.html'), 500

def create_app():
    app = Flask(__name__)
    app.config.from_object('config')

    from users.views import b_users
    db.init_app(app)
    app.register_blueprint(b_users)
    configure_errors(app)

    return app

如果有人链接到一个大型的blueprinted-appfactory实现的源代码,该代码使用WSGI中间件作为python Flask网站的子目录,那将会有很大的帮助。

我遇到了一些严重的循环导入问题或子目录init.py问题。

[Sat Nov 24 05:32:59 2012] [error] [client 127.0.0.1] Traceback (most recent call last):
[Sat Nov 24 05:32:59 2012] [error] [client 127.0.0.1]   File "/home/somefolder/webapps/applicationsubfolder/htdocs/index.py", line 8, in <module>
[Sat Nov 24 05:32:59 2012] [error] [client 127.0.0.1]     from app import create_app
[Sat Nov 24 05:32:59 2012] [error] [client 127.0.0.1]   File "/home/somefolder/webapps/applicationsubfolder/htdocs/app.py", line 5, in <module>
[Sat Nov 24 05:32:59 2012] [error] [client 127.0.0.1]     from users.views import b_users
[Sat Nov 24 05:32:59 2012] [error] [client 127.0.0.1]   File "/home/somefolder/webapps/applicationsubfolder/htdocs/users/__init__.py", line 9, in <module>
[Sat Nov 24 05:32:59 2012] [error] [client 127.0.0.1]     from index import application
[Sat Nov 24 05:32:59 2012] [error] [client 127.0.0.1]   File "/home/somefolder/webapps/applicationsubfolder/htdocs/index.py", line 8, in <module>
[Sat Nov 24 05:32:59 2012] [error] [client 127.0.0.1]     from app import create_app
[Sat Nov 24 05:32:59 2012] [error] [client 127.0.0.1] ImportError: cannot import name create_app

1 个答案:

答案 0 :(得分:1)

我不完全确定你的要求。我试试吧。

  1. 如果要在视图功能网址前添加蓝图文件夹名称前缀,请执行以下操作:

    app.register_blueprint(b_users,url_prefix ='/ folder_name')

    网址将为localhost/folder_name/url

  2. 在create_app方法中移动它。

    app.wsgi_app = WebFactionMiddleware(app.wsgi_app)