Пример #1
0
# demo start
from sanic import Sanic, Blueprint
from sanic.response import text

from sanic_limiter import Limiter, get_remote_address

app = Sanic(__name__)
limiter = Limiter(app, global_limits=['1 per hour', '10 per day'], key_func=get_remote_address)
bp = Blueprint('some_bp')
limiter.limit("2 per hour")(bp)


@bp.route("/bp1")
async def bp_t1(request):
    return text("bp_t1")


@app.route("/t1")
@limiter.limit("100 per hour;10/minute")
async def t1(request):
    return text("t1")


@app.route("/t2")
async def t2(request):
    return text("t2")


@app.route("/t3")
@limiter.exempt
async def t3(request):
Пример #2
0
app.blueprint(openapi_blueprint)
app.blueprint(swagger_blueprint)

redis = SanicRedis(app)
CORS(app, automatic_options=True)

app.blueprint(crud_bp)
# 接口访问限制
# app.config.RATELIMIT_STORAGE_URL = 'redis://127.0.0.1:6379'
limiter = Limiter(app,
                  global_limits=['10000 per hour', '100000 per day'],
                  key_func=get_remote_address,
                  # storage_uri='redis://localhost:6379/1'
                  )
limiter.init_app(app)
limiter.limit("1000 per hour")(crud_bp)
# limiter.exempt(crud_bp)


# JWT 配置
# jwt 返回jwt 获取token的键设置,将改变默认键 access_token
app.config.SANIC_JWT_ACCESS_TOKEN_NAME = 'sanic-token'
# app.config.SANIC_JWT_ACCESS_TOKEN_NAME = 'jwt'

# 设置过期时间, 默认30分钟
# app.config['JWT_EXPIRATION_DELTA'] = datetime.timedelta(days=10)
app.config['JWT_EXPIRATION_DELTA'] = datetime.timedelta(seconds=60)

initialize(
    app,
    authenticate=authenticate,