1

我正在尝试让 Falcon 在 Webfaction 上运行。我不完全是网络专家,所以我很难理解这些应用程序的服务方式。

我的 Webfaction 应用程序设置为mod_wsgi 4.5.3/Python 2.7

据我了解,Falcon 可以在任何 WSGI 服务器上运行。当我启动我的 mod_wsgi 服务器时,它是否会自动配置为运行 Falcon 之类的东西?还是我还需要安装 Gunicorn 之类的东西?

当我设置我的 webfaction 应用程序时,我收到了这样的目录结构:

app/htdocs/index.py

在 index.py 文件中,我放置了在Falcon Tutorial找到的示例

import falcon

class ThingsResource(object):
    def on_get(self, req, resp):
        """Handles GET requests"""
        resp.status = falcon.HTTP_200
        resp.body = 'Hello world!'

# falcon.API instances are callable WSGI apps
wsgi_app = api = falcon.API()

# Resources are represented by long-lived class instances
things = ThingsResource()

# things will handle all requests to the '/things' URL path
api.add_route('/things', things)

我知道也有运行 WSGI 的说明,但这就是我的困惑所在 - webfaction 服务器是否已经在运行 WSGI,或者我仍然需要像 Gunicorn 这样的东西,如果是这样 - 最好的配置方式是什么?我需要一个 cron 来继续运行 Gunicorn 吗?

谢谢!

更新:

我检查了错误日志并收到关于没有名为“应用程序”的变量的 WSGI 错误,

所以我改变了:

wsgi_app = api = falcon.API()

至:

application = falcon.API()

这清除了错误,但现在当我访问 mydomain.com/things 时,我收到错误 404(未找到/不存在)。

所以,这让我回到了最初的问题,即下一步是什么?似乎 url 没有被正确路由,所以它很可能与 httpd.conf 文件或类似文件有关 - 再次,这是我第一次尝试这样设置。

4

1 回答 1

0

这是答案(至少对于最初的问题,我敢打赌我会在不久的将来在同一个项目上搞砸其他事情)。

从本质上讲,我能够将教程代码放在 Webfaction 在设置应用程序和安装在域上时生成的 index.py 文件中。所以,我的教程代码看起来像这样:

import falcon

class ThingsResource(object):
        def on_get(self,req,resp):
                resp.status = falcon.HTTP_200
                resp.body = 'Hello World!'

api = application  = falcon.API()

things = ThingsResource()

api.add_route('/things', things)

由于我找不到太多关于在 Webfaction 上启动 Falcon 应用程序的信息,我查看了类似的应用程序是如何在 Webfaction 上运行的(本例中为 Flask)。话虽如此,我在 Flask 文档中找到了一个片段,展示了如何在 webfaction 上进行设置。我不确定这是否意味着我的整个应用程序都可以工作,但我知道 Falcon 教程代码至少可以工作。本质上,我只需要按照此处找到的说明编辑 httpd.conf 文件:Flask Webfaction

WSGIPythonPath /home/yourusername/webapps/yourapp/htdocs/
#If you do not specify the following directive the app *will* work but you will
#see index.py in the path of all URLs
WSGIScriptAlias / /home/yourusername/webapps/yourapp/htdocs/index.py

<Directory /home/yourusername/webapps/yourapp/htdocs/>
   AddHandler wsgi-script .py
   RewriteEngine on
   RewriteBase /
   WSGIScriptReloading On
</Directory>

我希望这可以帮助任何有类似问题的人。

于 2016-08-25T18:47:53.613 回答