基于类的烧瓶中的限制器

时间:2017-11-19 07:57:12

标签: python flask flask-restful

从这里: Python Rate Limit class based view Flask 我实施了解决方案:

from flask import Flask
from flask_limiter import Limiter
from flask_limiter.util import get_remote_address

limiter = Limiter(api_app, key_func=get_remote_address, global_limits=[settings.api_global_limits])
Status.method_decorators.append(limiter)
gonzo_api.add_resource(Status, '/api/1.0/status')

我的Status课程:

class Status(Resource):
    """Verifies API status."""

    @authenticator.db_authentication
    def get(self):
        """

        :return:
        """
        try:
            log.info('%s %r' % (request.remote_addr, request))
            log.info('api() | GET | Received request for Status')
            response = json.dumps('Status: GET. Hola %s!' % g.user.username)
            return Response(response, status=200, mimetype=settings.api_mime_type)
        except (TypeError, ValueError) as e:
            log.exception(e)
            response = json.dumps(errors.API_ERROR)
            return Response(response, status=500, mimetype=settings.api_mime_type)

相关代码:

https://github.com/flask-restful/flask-restful/blob/master/flask_restful/init.py#L574

错误1:

[2017-11-18 23:49:35 -0800] [95972] [DEBUG] GET /api/1.0/status
[2017-11-18 23:49:35,332] ERROR in app: Exception on /api/1.0/status [GET]
Traceback (most recent call last):
  File "/Users/spicyramen/Documents/OpenSource/Development/Python/gonzoenv/lib/python2.7/site-packages/flask/app.py", line 1612, in full_dispatch_request
    rv = self.dispatch_request()
  File "/Users/spicyramen/Documents/OpenSource/Development/Python/gonzoenv/lib/python2.7/site-packages/flask/app.py", line 1598, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/Users/spicyramen/Documents/OpenSource/Development/Python/gonzoenv/lib/python2.7/site-packages/flask_restful/__init__.py", line 477, in wrapper
    resp = resource(*args, **kwargs)
  File "/Users/spicyramen/Documents/OpenSource/Development/Python/gonzoenv/lib/python2.7/site-packages/flask/views.py", line 84, in view
    return self.dispatch_request(*args, **kwargs)
  File "/Users/spicyramen/Documents/OpenSource/Development/Python/gonzoenv/lib/python2.7/site-packages/flask_restful/__init__.py", line 585, in dispatch_request
    meth = decorator(meth)
TypeError: 'Limiter' object is not callable

如果我将以下代码添加到Status类,则会失败:

@property
def method_decorators(self):
    # get some limiter bound to the `g` context
    return g.limiter

错误2: 错误似乎与Status.method_decorators.append有关,因为method_decorators是[],当我们调用它时,我们调用的是getter,而不是setter。

[2017-11-18 23:55:00 -0800] [3228] [ERROR] Exception in worker process
Traceback (most recent call last):
  File "/Users/spicyramen/Documents/OpenSource/Development/Python/gonzoenv/lib/python2.7/site-packages/gunicorn/arbiter.py", line 557, in spawn_worker
    worker.init_process()
  File "/Users/spicyramen/Documents/OpenSource/Development/Python/gonzoenv/lib/python2.7/site-packages/gunicorn/workers/base.py", line 126, in init_process
    self.load_wsgi()
  File "/Users/spicyramen/Documents/OpenSource/Development/Python/gonzoenv/lib/python2.7/site-packages/gunicorn/workers/base.py", line 136, in load_wsgi
    self.wsgi = self.app.wsgi()
  File "/Users/spicyramen/Documents/OpenSource/Development/Python/gonzoenv/lib/python2.7/site-packages/gunicorn/app/base.py", line 67, in wsgi
    self.callable = self.load()
  File "/Users/spicyramen/Documents/OpenSource/Development/Python/gonzoenv/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py", line 65, in load
    return self.load_wsgiapp()
  File "/Users/spicyramen/Documents/OpenSource/Development/Python/gonzoenv/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py", line 52, in load_wsgiapp
    return util.import_app(self.app_uri)
  File "/Users/spicyramen/Documents/OpenSource/Development/Python/gonzoenv/lib/python2.7/site-packages/gunicorn/util.py", line 357, in import_app
    __import__(module)
  File "/Users/spicyramen/Documents/OpenSource/Development/Python/gonzo/api/version1_0/gonzo_api.py", line 13, in <module>
    from application.app import api_app
  File "/Users/spicyramen/Documents/OpenSource/Development/Python/gonzo/api/version1_0/application/app.py", line 74, in <module>
    Status.method_decorators.append(limiter)
AttributeError: 'property' object has no attribute 'append'

1 个答案:

答案 0 :(得分:1)

根据docs您需要进行装饰的方法是limit所以可能会这样做:

Status.method_decorators.append(limiter.limit('50 per hour'))

找到速率限制字符串格式here