简体   繁体   中英

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 WSGIApplication 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:

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 :

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM