开发者

GAE CGI: how to response http status code

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 WSGIApplic开发者_Go百科ation and then response a status code.
    return

  application = webapp.WSGIApplication([
    (r'/', MainHandler),
    #...
    ], debug=True)
  run_wsgi_app(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:

  • https://stackoverflow.com/questions/tagged/google-app-engine

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

  • http://code.google.com/appengine/docs/python/tools/webapp/

Check how to set response object status code at

  • http://code.google.com/appengine/docs/python/tools/webapp/redirects.html

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 :

  • http://code.google.com/p/pubsubhubbub/source/browse/trunk/hub/main.py?spec=svn335&r=146#1228
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜