Falcon框架应用在gunicorn下无法运行

0 投票
1 回答
1853 浏览
提问于 2025-04-18 11:08

我有一个简单的Falcon应用:

import falcon

class ThingsResource:
    def on_get(self, req, resq) :
        #"""Handels GET requests"""
        resp.status = falcon.HTTP_200
                resp.body = '{"message":"hello"}'


app = falcon.API()
things = ThingsResource()
app.add_route('/things', things)

我想用gunicorn来运行它,方法是:

arif@ubuntu:~/dialer_api$ gunicorn things:app

但是当我尝试用httpie连接时,出现了这个问题:

arif@ubuntu:~$ http localhost:8000/things
HTTP/1.1 500 Internal Server Error
Connection: close
Content-Length: 141
Content-Type: text/html

<html>
  <head>
    <title>Internal Server Error</title>
  </head>
  <body>
    <h1><p>Internal Server Error</p></h1>

  </body>
</html>

这真的很简单,我不知道哪里出错了?

1 个回答

3

你第七行的缩进不应该存在。

你第六行提到了resq,而不是后面用到的resp。这就导致后面的代码在提到resp时出错了。

每当你遇到这样的内部服务器错误,通常都是因为代码有问题。你的gunicorn进程应该会输出错误信息。我附上了你代码的修正版本,并确保它符合Python的规范(比如每次缩进用4个空格)。像在线Python代码检查工具这样的工具,可以帮助你检查像这样的代码片段,特别是缩进问题。

import falcon


class ThingsResource():
    def on_get(self, req, resp):
        resp.status = falcon.HTTP_200
        resp.body = '{"message":"hello"}'


app = falcon.API()
things = ThingsResource()
app.add_route('/things', things)

把这段代码保存到另一个文件里,然后运行diff命令,看看我改了什么。主要问题就是resp和不当的缩进。

撰写回答