Falcon太早关闭连接

时间:2016-03-31 11:48:33

标签: python gunicorn falconframework

Falcon框架永远不会返回整个响应。我从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

这是路由处理程序类。我查了一下,_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

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

1 个答案:

答案 0 :(得分:2)

使用gunicorn index:app --log-level DEBUG启动你的gunicorn工作人员。然后,再次尝试你的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)