是否有一种简单的方法可以使用状态码429中止?

时间:2012-07-01 00:21:14

标签: python flask

尝试使用该代码从“before_request”处理程序(在Flask中)中止时会出现以下错误:LookupError: no exception for 429。有办法解决这个问题吗?

以下是触发错误的代码:

key = "ip:{}:account-requests".format(request.remote_addr)

# Determine how many requests have been made in the past day
# by this API.
requests_made = r.get(key) or 0

if requests_made >= max_requests:
    abort(429) #HTTP Too Many Requests

1 个答案:

答案 0 :(得分:4)

你可以在Flask

中这样做
if requests_made >= max_requests:
    return '429 error', 429