今天就跟大家聊聊有關(guān)Django中怎么限制訪問(wèn)頻率,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
按需網(wǎng)站策劃可以根據(jù)自己的需求進(jìn)行定制,網(wǎng)站設(shè)計(jì)制作、成都網(wǎng)站制作構(gòu)思過(guò)程中功能建設(shè)理應(yīng)排到主要部位公司網(wǎng)站設(shè)計(jì)制作、成都網(wǎng)站制作的運(yùn)用實(shí)際效果公司網(wǎng)站制作網(wǎng)站建立與制做的實(shí)際意義最近做了一個(gè)系統(tǒng)由于部分接口需要進(jìn)行耗時(shí)操作,因而不希望用戶進(jìn)行頻繁訪問(wèn),需要進(jìn)行訪問(wèn)頻率限制。如果要自己實(shí)現(xiàn)一個(gè)訪問(wèn)限制功能相對(duì)來(lái)說(shuō)也不會(huì)太復(fù)雜,并且網(wǎng)上有各種代碼可以參考。如果自己不想實(shí)現(xiàn)這個(gè)代碼可以使用 Django Ratelimit 。
Django Ratelimit is a ratelimiting decorator for Django views. /file/tupian/20230213/16288.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/
使用方法也相對(duì)來(lái)說(shuō)比較簡(jiǎn)單:
@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()
不過(guò)需要注意如果和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):
否則會(huì)導(dǎo)致如下的錯(cuò)誤信息:
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
看完上述內(nèi)容,你們對(duì)Django中怎么限制訪問(wèn)頻率有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。
當(dāng)前標(biāo)題:Django中怎么限制訪問(wèn)頻率-創(chuàng)新互聯(lián)
鏈接地址:http://jinyejixie.com/article18/djspdp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供關(guān)鍵詞優(yōu)化、移動(dòng)網(wǎng)站建設(shè)、網(wǎng)站設(shè)計(jì)公司、品牌網(wǎng)站設(shè)計(jì)、商城網(wǎng)站、Google
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容