猎鹰过早关闭连接

Falcon closing the connection too early

Falcon 框架从未 returns 整个响应。我从 curl(或任何其他 HTTP 工具)得到的是:

$ curl -q -D - -o - "localhost:8000/post_account?email=someone@example.com

HTTP/1.1 200 OK
Server: gunicorn/19.4.5
Date: Thu, 31 Mar 2016 11:36:49 GMT
Connection: close
content-length: 3
content-type: application/json; charset=utf-8

curl: (18) transfer closed with 3 bytes remaining to read

index.py

这是定义路由的引导脚本。

import falcon
from routes import route_account


app = falcon.API()
post_account = route_account.RoutePostAccount()

# Routes
app.add_route('/post_account', post_account)

route_account.py

这是路由处理程序 class。我检查了 _result = account.create_account(**_payload) 收到的结果是好的。

from falcon.util import uri
from objects.account_base import AccountBase

account = AccountBase()


class RoutePostAccount(object):

    @staticmethod
    def on_get(req, resp):

        # Convert query parameters string to dict
        _payload = uri.parse_query_string(req.query_string)
        # Create account
        _result = account.create_account(**_payload)
        # Send response
        resp.status = _result.get('status', {}).get('code')
        resp.body = _result

网络服务器

$ gunicorn index:app

有人看到我看不到的东西吗?感谢您的帮助。

使用gunicorn index:app --log-level DEBUG启动你的gunicorn workers。然后,再次尝试您的 curl 命令并查看 gunicorn 控制台:一定有错误。我认为 account.create_account(**_payload) 正在返回无效的内容。示例:如果您 运行 它在 python 3 上,那么它必须返回一个字典,但 falcon 需要一个字节字符串,因此它会失败并关闭连接。您必须序列化(到 json、xml,无论您喜欢什么)account.create_account(**_payload) 的输出。这样:

route_account.py:

import json

from falcon.util import uri
from objects.account_base import AccountBase

account = AccountBase()


class RoutePostAccount(object):

    @staticmethod
    def on_get(req, resp):

        # Convert query parameters string to dict
        _payload = uri.parse_query_string(req.query_string)
        # Create account
        _result = account.create_account(**_payload)
        # Send response
        resp.status = _result.get('status', {}).get('code')
        resp.body = json.dumps(_result)