如何在 Webapp2 中 return 状态码 418

How to return status code 418 in Webapp2

Webapp2 似乎不支持状态代码 418,因为它没有映射到它的状态消息。我该如何绕过它 return a 418?

response.set_status(418)    # does not work
response.status_int = 418   # does not work either

回溯:

  File "/.../App Engine SDK/lib/webapp2-2.5.2/webapp2.py", line 425, in set_status
    self.status = code
  File "/.../App Engine SDK/lib/webapp2-2.5.2/webapp2.py", line 405, in _set_status
    message = message or Response.http_status_message(code)
  File "/.../App Engine SDK/lib/webapp2-2.5.2/webapp2.py", line 488, in http_status_message
    raise KeyError('Invalid HTTP status code: %d' % code)
KeyError: 'Invalid HTTP status code: 418'

通过猴子补丁,我能够让状态码 418 工作,但只是部分工作。

class TeapotHandle(webapp2.RequestHandler):
    def get(self):
        from webob import util
        import httplib

        util.status_reasons[418] = "I'm a teapot"
        httplib.responses[418] = "I'm a teapot"

        self.response.set_status(418)

产量,使用开发服务器:

HTTP/1.1 418 Unknown Status Code
[...]

但实际上,一旦部署到 App Engine Cloud 上,它就会按预期工作:

HTTP/1.1 418 I'm a Teapot
[...]