亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Django 限制訪問頻率的思路詳解

 更新時間:2019年12月24日 11:27:36   作者:h4ck  
這篇文章主要介紹了Django 限制訪問頻率的思路詳解,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下

最近做了一個系統(tǒng)由于部分接口需要進行耗時操作,因而不希望用戶進行頻繁訪問,需要進行訪問頻率限制。如果要自己實現(xiàn)一個訪問限制功能相對來說也不會太復(fù)雜,并且網(wǎng)上有各種代碼可以參考。如果自己不想實現(xiàn)這個代碼可以使用 Django Ratelimit 。

Django Ratelimit is a ratelimiting decorator for Django views.
https://travis-ci.org/jsocol/django-ratelimit.png?branch=master Code: https://github.com/jsocol/django-ratelimit License: Apache Software License Issues: https://github.com/jsocol/django-ratelimit/issues Documentation: http://django-ratelimit.readthedocs.org/

使用方法也相對來說比較簡單:

@ratelimit(key='ip', rate='5/m')
def myview(request):
  # Will be true if the same IP makes more than 5 POST
  # requests/minute.
  was_limited = getattr(request, 'limited', False)
  return HttpResponse()
@ratelimit(key='ip', rate='5/m', block=True)
def myview(request):
  # If the same IP makes >5 reqs/min, will raise Ratelimited
  return HttpResponse()
@ratelimit(key='post:username', rate='5/m', method=['GET', 'POST'])
def login(request):
  # If the same username is used >5 times/min, this will be True.
  # The `username` value will come from GET or POST, determined by the
  # request method.
  was_limited = getattr(request, 'limited', False)
  return HttpResponse()
@ratelimit(key='post:username', rate='5/m')
@ratelimit(key='post:tenant', rate='5/m')
def login(request):
  # Use multiple keys by stacking decorators.
  return HttpResponse()
@ratelimit(key='get:q', rate='5/m')
@ratelimit(key='post:q', rate='5/m')
def search(request):
  # These two decorators combine to form one rate limit: the same search
  # query can only be tried 5 times a minute, regardless of the request
  # method (GET or POST)
  return HttpResponse()
@ratelimit(key='ip', rate='4/h')
def slow(request):
  # Allow 4 reqs/hour.
  return HttpResponse()
rate = lambda r: None if request.user.is_authenticated else '100/h'
@ratelimit(key='ip', rate=rate)
def skipif1(request):
  # Only rate limit anonymous requests
  return HttpResponse()
@ratelimit(key='user_or_ip', rate='10/s')
@ratelimit(key='user_or_ip', rate='100/m')
def burst_limit(request):
  # Implement a separate burst limit.
  return HttpResponse()
@ratelimit(group='expensive', key='user_or_ip', rate='10/h')
def expensive_view_a(request):
  return something_expensive()
@ratelimit(group='expensive', key='user_or_ip', rate='10/h')
def expensive_view_b(request):
  # Shares a counter with expensive_view_a
  return something_else_expensive()
@ratelimit(key='header:x-cluster-client-ip')
def post(request):
  # Uses the X-Cluster-Client-IP header value.
  return HttpResponse()
@ratelimit(key=lambda r: r.META.get('HTTP_X_CLUSTER_CLIENT_IP',
                  r.META['REMOTE_ADDR'])
def myview(request):
  # Use `X-Cluster-Client-IP` but fall back to REMOTE_ADDR.
  return HttpResponse()

不過需要注意如果和django rest framwork一起使用的話,要將Ratelimit 裝飾器放到第一行,如下:

@ratelimit(key='user', rate='1/3s', block=True, method=ratelimit.ALL)
@api_view(['POST', 'GET'])
@csrf_exempt
def api_get_level(request):

否則會導(dǎo)致如下的錯誤信息:

IndexError at /rest-api/level/
tuple index out of range
Request Method: GET
Request URL: http://192.168.1.195:8006/rest-api/level/
Django Version: 2.2.7
Exception Type: IndexError
Exception Value: 
tuple index out of range
Exception Location: F:\PyCharmProjects\server\venv\lib\site-packages\ratelimit\decorators.py in _wrapped, line 23
Python Executable: F:\PyCharmProjects\server\venv\Scripts\python.exe
Python Version: 3.7.5
Python Path: 
['F:\\PyCharmProjects\\server\\TaichiGameServer',
 'I:\\Python37-64\\python37.zip',
 'I:\\Python37-64\\DLLs',
 'I:\\Python37-64\\lib',
 'I:\\Python37-64',
 'F:\\PyCharmProjects\\server\\venv',
 'F:\\PyCharmProjects\\server\\venv\\lib\\site-packages',
 'F:\\PyCharmProjects\\server\\venv\\lib\\site-packages\\setuptools-39.1.0-py3.7.egg']
Server time: Tue, 24 Dec 2019 09:49:01 +0800
 
Traceback (most recent call last):
 File "F:\PyCharmProjects\server\venv\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
  response = get_response(request)
 File "F:\PyCharmProjects\server\venv\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response
  response = self.process_exception_by_middleware(e, request)
 File "F:\PyCharmProjects\server\venv\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response
  response = wrapped_callback(request, *callback_args, **callback_kwargs)
 File "F:\PyCharmProjects\server\venv\lib\site-packages\django\views\decorators\csrf.py", line 54, in wrapped_view
  return view_func(*args, **kwargs)
 File "F:\PyCharmProjects\server\venv\lib\site-packages\django\views\generic\base.py", line 71, in view
  return self.dispatch(request, *args, **kwargs)
 File "F:\PyCharmProjects\server\venv\lib\site-packages\rest_framework\views.py", line 505, in dispatch
  response = self.handle_exception(exc)
 File "F:\PyCharmProjects\server\venv\lib\site-packages\rest_framework\views.py", line 465, in handle_exception
  self.raise_uncaught_exception(exc)
 File "F:\PyCharmProjects\server\venv\lib\site-packages\rest_framework\views.py", line 476, in raise_uncaught_exception
  raise exc
 File "F:\PyCharmProjects\server\venv\lib\site-packages\rest_framework\views.py", line 502, in dispatch
  response = handler(request, *args, **kwargs)
 File "F:\PyCharmProjects\server\venv\lib\site-packages\rest_framework\decorators.py", line 50, in handler
  return func(*args, **kwargs)
 File "F:\PyCharmProjects\server\venv\lib\site-packages\django\views\decorators\csrf.py", line 54, in wrapped_view
  return view_func(*args, **kwargs)
 File "F:\PyCharmProjects\server\venv\lib\site-packages\ratelimit\decorators.py", line 23, in _wrapped
  request = args[1]
IndexError: tuple index out of range

總結(jié)

以上所述是小編給大家介紹的Django 限制訪問頻率的思路詳解,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉(zhuǎn)載,煩請注明出處,謝謝!

相關(guān)文章

  • Python的shutil模塊中文件的復(fù)制操作函數(shù)詳解

    Python的shutil模塊中文件的復(fù)制操作函數(shù)詳解

    shutil被定義為Python中的一個高級的文件操作模塊,擁有比os模塊中更強大的函數(shù),這里我們就來看一下Python的shutil模塊中文件的復(fù)制操作函數(shù)詳解
    2016-07-07
  • pytorch實現(xiàn)focal loss的兩種方式小結(jié)

    pytorch實現(xiàn)focal loss的兩種方式小結(jié)

    今天小編就為大家分享一篇pytorch實現(xiàn)focal loss的兩種方式小結(jié),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-01-01
  • python算的上腳本語言嗎

    python算的上腳本語言嗎

    在本篇內(nèi)容中小編給大家分享的是關(guān)于python是否屬于腳本語言的相關(guān)內(nèi)容知識點,有興趣的朋友們可以參考下。
    2020-06-06
  • Django開發(fā)中復(fù)選框用法示例

    Django開發(fā)中復(fù)選框用法示例

    這篇文章主要介紹了Django開發(fā)中復(fù)選框用法,結(jié)合實例形式分析了Django基于ajax的復(fù)選框遍歷、提交及后臺數(shù)據(jù)庫查詢等相關(guān)操作技巧,需要的朋友可以參考下
    2018-03-03
  • Python 使用Numpy對矩陣進行轉(zhuǎn)置的方法

    Python 使用Numpy對矩陣進行轉(zhuǎn)置的方法

    今天小編就為大家分享一篇Python 使用Numpy對矩陣進行轉(zhuǎn)置的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-01-01
  • Django項目定期自動清除過期session的2種方法實例

    Django項目定期自動清除過期session的2種方法實例

    如果用戶主動退出,session會自動清除,如果沒有退出就一直保留,記錄數(shù)越來越大,要定時清理沒用的session,下面這篇文章主要給大家介紹了關(guān)于Django項目定期自動清除過期session的2種方法,需要的朋友可以參考下
    2022-08-08
  • 關(guān)于python中的xpath解析定位

    關(guān)于python中的xpath解析定位

    這篇文章主要介紹了關(guān)于python中的xpath解析定位,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-03-03
  • python打印帶時間的日志實現(xiàn)代碼

    python打印帶時間的日志實現(xiàn)代碼

    python的logging模塊提供了標準的日志接口,可以通過它存儲各種格式的日志,下面這篇文章主要給大家介紹了關(guān)于python打印帶時間的日志的相關(guān)資料,文中通過代碼介紹的非常詳細,需要的朋友可以參考下
    2024-04-04
  • opencv python統(tǒng)計及繪制直方圖的方法

    opencv python統(tǒng)計及繪制直方圖的方法

    這篇文章主要介紹了opencv python統(tǒng)計及繪制直方圖的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-01-01
  • python?泛型函數(shù)--singledispatch的使用解讀

    python?泛型函數(shù)--singledispatch的使用解讀

    這篇文章主要介紹了python?泛型函數(shù)--singledispatch的使用解讀,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-09-09

最新評論