GAE CGI:如何响应http状态代码

发布于 2024-09-28 17:03:17 字数 762 浏览 1 评论 0原文

我正在使用googleappening,它是CGI环境。我想阻止一些请求,我不想响应任何内容,甚至没有 http 状态代码。或者,我只想关闭连接。我可以这样做吗?

更新:

我决定使用 pyfunc 所说的,使用 204 状态,但是我如何在 GAE CGI 环境没有任何网络框架的情况下做到这一点。

更新 2:

非常感谢,但是......我真的需要CGI 方式,而不是 WSGI 方式。请参阅我的代码中的评论。

def main()
  #Block requests at once.
  if (settings.BLOCK_DOWNLOAD and os.environ.get('HTTP_RANGE')) \
        or (settings.BLOCK_EXTENSION.match(os.environ['PATH_INFO'])):
    #TODO: return 204 response in CGI way.
    #I really do not need construct a WSGIApplication and then response a status code.
    return

  application = webapp.WSGIApplication([
    (r'/', MainHandler),
    #...
    ], debug=True)
  run_wsgi_app(application)


if __name__ == '__main__':
  main()

I am using google appening, it's CGI environment. I want block some request, I want response nothing even no http status code. Alternative, I want just close the connection. Can I do this?

update:

I have decide to use what pyfunc said, use 204 status, but how can I do this at GAE CGI environment without any webframework.

update 2:

Thanks a lot, but... I really need a CGI way, not WSGI way. Please see the comment in my codes.

def main()
  #Block requests at once.
  if (settings.BLOCK_DOWNLOAD and os.environ.get('HTTP_RANGE')) \
        or (settings.BLOCK_EXTENSION.match(os.environ['PATH_INFO'])):
    #TODO: return 204 response in CGI way.
    #I really do not need construct a WSGIApplication and then response a status code.
    return

  application = webapp.WSGIApplication([
    (r'/', MainHandler),
    #...
    ], debug=True)
  run_wsgi_app(application)


if __name__ == '__main__':
  main()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暗藏城府 2024-10-05 17:03:17

HTTP 状态代码在响应中很重要

  1. 您可以使用 HTTP NO CONTENT 204 以空内容进行响应。
  2. 您可以使用 403 Forbidden,但我更喜欢 204 来发出静默的请求。
  3. 您可以断开连接,但这很粗鲁,并且可能会导致服务器因用户可能重试而受到连接的冲击。

[编辑:更新的问题]

您可以查看许多用 GAE 标记的 SO 示例:

据我了解,您将使用 webapp 框架。加强它的使用。

中查看如何设置响应对象状态代码

下面是一个简单服务器的示例,它响应 204 no content。我还没有测试过它,但它会是类似的。

import wsgiref.handlers 
from google.appengine.ext import webapp 
class MainHandler(webapp.RequestHandler):
    def get(self): 
        return self.response.set_status(204)

def main(): 
    application = webapp.WSGIApplication([('/', MainHandler)], debug=True)
    wsgi.handlers.CGIHandler().run(application)

if __name__ == '__main__': 
    main()

请访问以下网址查看完整的申请:

The HTTP status code is important in response

  1. You can use HTTP NO CONTENT 204 to responde with empty content.
  2. You could use 403 Forbidden but I prefer 204 to make a silent drop of request
  3. You could loose connection but that would be rude and can result in server being pounded with connections as user may retry.

[Edit: updated question]

You can look at many a examples on SO tagged with GAE:

It is my understanding that you will be using webapp framework. Beef up on it's usage.

Check how to set response object status code at

Here is an example of bare bone server that responds with 204 no content. I have not tested it, but it would be in similar lines.

import wsgiref.handlers 
from google.appengine.ext import webapp 
class MainHandler(webapp.RequestHandler):
    def get(self): 
        return self.response.set_status(204)

def main(): 
    application = webapp.WSGIApplication([('/', MainHandler)], debug=True)
    wsgi.handlers.CGIHandler().run(application)

if __name__ == '__main__': 
    main()

See a complete application at :

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文