views:

44

answers:

1

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()
+3  A: 

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 :

pyfunc
thank you. I still need to do this at GAE cgi environment.
guilin 桂林
403 forbidden makes a lot more sense than 204 no body.
Nick Johnson
@Nick Johnson: Yes, But we had to block certain bad clients, we have used 204 No Content
pyfunc
@pyfunc: But why aren't you using 403 instead? It'll still block them, but it's more correct. Also, don't forget about the DoS API, which does this for you.
Nick Johnson