Created
September 24, 2013 10:20
-
-
Save mdornseif/6682867 to your computer and use it in GitHub Desktop.
Limiting certain countries
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class RateLimitTimeMiddleware(object): | |
def __init__(self, app): | |
self.app = app | |
def __call__(self, environ, start_response): | |
if environ.get('HTTP_X_APPENGINE_COUNTRY', '??') in ['KW', 'CN']: | |
start_response('429 Too Many Requests', [('Content-type', 'text/html')]) | |
return ["""<html><head><title>429 Too Many Requests, sorry...</title> | |
<style>body { font-family: verdana, arial, sans-serif; background-color: #fff; color: #000; }</style> | |
</head><body><div style="margin-left: 4em;"><h1>We're sorry...</h1> | |
<p>... but your computer or network may be sending automated queries. | |
To protect our users, we can't process your request right now.</p></body></html>"""] | |
return self.app(environ, start_response) | |
def webapp_add_wsgi_middleware(app): | |
"""Called with each WSGI handler initialisation """ | |
app = gaetk.gaesessions.SessionMiddleware(app, cookie_key=COOKIE_KEY) | |
app = frontend.redirects.middleware.RedirectMiddleware(app) | |
# rate Limiting should be the last one so it is executed at first. | |
app = RateLimitTimeMiddleware(app) | |
return app |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment