def process_request(self, req, res): requester = req.env['REMOTE_ADDR'] # un-comment if you want to ignore calls from localhost # if requester == '127.0.0.1': # return key = "{0}: {1}".format(requester, req.path) print('Key: {0}'.format(key)) try: remaining = self.limit - int(self.redis.get(key)) except (ValueError, TypeError): remaining = self.limit self.redis.set(key, 0) expires_in = self.redis.ttl(key) if expires_in == -1: self.redis.expire(key, self.window) expires_in = self.window res.append_header('X-RateLimit-Remaining: ', str(remaining - 1)) res.append_header('X-RateLimit-Limit: ', str(self.limit)) res.append_header('X-RateLimit-Reset: ', str(time() + expires_in)) if remaining > 0: self.redis.incr(key, 1) else: raise falcon.HTTPTooManyRequests( title='Rate Limit Hit', description='Blocked: Too many requests')
def test_http_too_many_requests_no_title_and_desc_and_challenges(self): try: raise falcon.HTTPTooManyRequests() except falcon.HTTPTooManyRequests as e: self.assertEqual(status.HTTP_429, e.title, 'The title should be ' + status.HTTP_429 + ', but it is: ' + e.title) self.assertEqual(None, e.description, 'The description should be None') self.assertNotIn('Retry-After', e.headers, 'Retry-After should not be in the headers')
def test_http_too_many_requests_with_title_and_desc_and_challenges(self): try: raise falcon.HTTPTooManyRequests(title='Test', description='Testdescription', retry_after=123) except falcon.HTTPTooManyRequests as e: self.assertEqual('Test', e.title, 'Title should be "Test"') self.assertEqual('Testdescription', e.description, 'Description should be "Testdescription"') self.assertEqual('123', e.headers['Retry-After'], 'Retry-After should be 123')
def process_request(self, req, resp): ip_address = IPv4Address(req.access_route[0]) req_time = datetime.now() nw_address = IPv4Network( "{ip_address}/{ip_mask}".format(ip_address=ip_address.compressed, ip_mask=self._ip_mask), strict=False, ) limiting_req_time = self._ip_networks[nw_address].responses[0] limiting_req_timedelta = req_time - limiting_req_time self._ip_networks[nw_address].responses.append(req_time) if limiting_req_timedelta < self._limiting_period: raise falcon.HTTPTooManyRequests()
def on_get(self, req, resp): raise falcon.HTTPTooManyRequests( title='Too many requests', description='1 per minute', retry_after=self.retry_after, )
def on_get(self, req, resp): raise falcon.HTTPTooManyRequests('Too many requests', '1 per minute', retry_after=self.retry_after)
def _rate_db(req, resp, argument): if _RateLimitDB.check_for(req.forwarded_host, argument): print("RATE_LIMITED") resp.status = falcon.HTTP_429 raise falcon.HTTPTooManyRequests(argument.error_message)
def hook(req, resp, resource, params): if _RateLimitDB.check_for(req.forwarded_host, resource, window_size) > per_second: resp.status = falcon.HTTP_429 raise falcon.HTTPTooManyRequests(error_message)
def _rate_redis(req, resp, argument): broker = redis.StrictRedis.from_url(argument.redis_url) if _RateLimitDBRedis.check_for(req.forwarded_host, argument, broker): resp.status = falcon.HTTP_429 raise falcon.HTTPTooManyRequests(argument.error_message)