trying to send HTTP ERROR 429 for a certain user agent

62 views
Skip to first unread message

Kate

unread,
Aug 2, 2012, 3:53:50 PM8/2/12
to google-a...@googlegroups.com
How can I trap a certain user agent and send a 429 response.

I have the following in my appengine_config.py and it always defaults to a 200.

(Note I do not intend to block all Windows user agent requests bit have the code "sindex = string.find(ua,'Win',0)" there for testing only).


from webob import Response
import os

class AntiCurlMiddleware(object):
    def __init__(self, app):
        self.app = app

    def __call__(self, environ, start_response):
        ua = os.environ.get('HTTP_USER_AGENT', "unknown")
sindex = string.find(ua,'Win',0)
 
if sindex > 0:
     resp = Response('Too many requests!')
     resp.status_code =429
     return resp(environ, start_response)
        else:
           return self.app(environ, start_response)


def webapp_add_wsgi_middleware(app):
    return AntiCurlMiddlewarey(app)

hyperflame

unread,
Aug 2, 2012, 4:21:05 PM8/2/12
to Google App Engine
Did you spell this correctly:

>     return AntiCurlMiddlewarey(app)

There's an extra "y" there. Looks like the real function is named
AntiCurlMiddleware without the y.
Reply all
Reply to author
Forward
0 new messages