我如何使用与Apache和mod_wsgi的瓶路线? [英] How do I use Flask routes with Apache and mod_wsgi?

查看:184
本文介绍了我如何使用与Apache和mod_wsgi的瓶路线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的Apache服务器的设置,它是通过处理的mod_wsgi瓶响应。我已经通过别名注册的WSGI脚本:

I've got my Apache server setup and it is handling Flask responses via mod_wsgi. I've registered the WSGI script via the alias:

[httpd.conf文件]

[httpd.conf]

WSGIScriptAlias /service "/mnt/www/wsgi-scripts/service.wsgi"

我已经添加了相应的WSGI文件在上面的路径:

I've added the corresponding WSGI file at the above path:

[/ MNT / WWW / WSGI的脚本/ service.wsgi]

[/mnt/www/wsgi-scripts/service.wsgi]

import sys
sys.path.insert(0, "/mnt/www/wsgi-scripts")

from service import application

和我有一个简单的测试瓶Python脚本,所提供的服务模块:

And I have a simple test Flask Python script that provides the service module:

[/ MNT / WWW / WSGI的脚本/ service.py]

[/mnt/www/wsgi-scripts/service.py]

from flask import Flask

app = Flask(__name__)

@app.route('/')
def application(environ, start_response):
        status = '200 OK'
        output = "Hello World!"
        response_headers = [('Content-type', 'text/plain'),
                            ('Content-Length', str(len(output)))]
        start_response(status, response_headers)
        return [output]

@app.route('/upload')
def upload(environ, start_response):
        output = "Uploading"
        status = '200 OK'
        response_headers = [('Content-type', 'text/plain'),
                            ('Content-Length', str(len(output)))]
        start_response(status, response_headers)
        return [output]

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

当我去我的网站URL [主机名] /服务它能正常工作,我得到的Hello World!背部。问题是,我不知道怎么去其他路线的工作一样,喜欢在上面的例子上传。这在独立的瓶不错,但下的mod_wsgi我难倒。我能想象的仅仅是注册在httpd.conf每个端点我想要一个单独WSGI脚本别名,但夺瓶的喜爱路由支持。有没有一种方法,使这项工作?

When I go to my website URL [hostname]/service it works as expected and I get "Hello World!" back. The problem is that I don't know how to get other routes to work like, like 'upload' in the example above. This works fine in standalone Flask but under mod_wsgi I'm stumped. The only thing I can imagine is registering a separate WSGI script alias in the httpd.conf for each endpoint I want, but that takes away Flask's fancy routing support. Is there a way to make this work?

推荐答案

在您的WSGI文件,你正在做的从服务导入应用程序,这是仅导入你的应用方法。

In your wsgi file you are doing from service import application, which is importing only your application method.

如预期改变,要从服务导入应用程序为这样就可以了。

Change that to from service import app as application and everything will work as expected.

您的评论后,我想我会扩大回答一下:

After your comment, I thought I'd expand the answer a bit:

您WSGI文件是蟒蛇code - 你可以有这个文件中的任何有效的Python code。安装在阿帕奇的WSGI经理人正在寻找的应用程序的名字在这个文件中,它将手断请求。烧瓶类实例 - 应用程序=瓶(__名__) - 提供这样的接口,但由于其名为应用而不是应用,你有,当你将其导入以它的别名 - 这就是从该行确实

Your wsgi file is python code - you can have any valid python code inside this file. The wsgi "handler" that is installed in Apache is looking for the application name in this file, which it will hand off requests to. A Flask class instance - app = Flask(__name__) - provides such an interface, but since its called app and not application, you have to alias it when you import it - that's what the from line does.

您可以 - 这是完全没有问题 - 只要做到这一点应用程序=瓶(__名__),然后指向Apache中的WSGI处理程序到你的 service.py 文件。如果 service.py 是导入的(这意味着,在某处 PYTHONPATH ),你就不需要中介WSGI脚本

You could - and this is perfectly fine - simply do this application = Flask(__name__) and then point the wsgi handler in Apache to your service.py file. If service.py was importable (that means, somewhere in PYTHONPATH), you wouldn't need an intermediary wsgi script.

虽然上面的作品,它是不好的做法。该WSGI文件需要从Apache进程工作许可;你通常分开,从实际的源$ C ​​$ C这应该是别的地方在你的文件系统,具有相应的权限。

Although the above works, its bad practice. The wsgi file needs permissions from the Apache process to work; and you generally separate that from the actual source code which should be somewhere else on your filesystem, with appropriate permissions.

这篇关于我如何使用与Apache和mod_wsgi的瓶路线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆