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
Web服务器
$ gunicorn index:app
有人看到我没看到的东西吗?谢谢你的帮助。
使用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)