Django limit the number of requests per minute

2024/10/11 2:22:07

I'm trying to limit the number of requests from an IP in case I get too many requests from it.

For example: if I will get more than 50 requests per minute I want to block that IP for 5 minutes.

When I use request.META['REMOTE_ADDR'] I always get the IP of the local host and not the one that sent the request.

  1. How can I get the IP of the computer that sent the request?
  2. How can I limit that IP to not send more requests for X time?
Answer

django-ratelimit will limit the number of requests you receive over a given amount of time.

Install:

pip install django-ratelimit

In your view:

from django_ratelimit.decorators import ratelimit@ratelimit(key='ip', rate='10/m')
def myview(request):
...
https://en.xdnf.cn/q/69821.html

Related Q&A

How to add template variable in the filename of an EmailOperator task? (Airflow)

I cant seem to get this to work.I am trying to send daily a given file, whose name is like file_{{ds_nodash}}.csv.The problem is that I cant seem to add this name as the filename, since it seems it can…

Disadvantage of Python eggs?

Are there any disadvantages about using eggs through easy-install compared to the "traditional" packages/modules/libs?

Does Google App Engine Flex support Pipfile?

For App Engine Standard the explicitly state that they do not support Pipfiles and immediately block you from pushing your project if it contains a Pipfile. In searching the documentation, I dont see a…

Keras sees my GPU but doesnt use it when training a neural network

My GPU is not used by Keras/TensorFlow.To try to make my GPU working with tensorflow, I installed tensorflow-gpu via pip (I am using Anaconda on Windows)I have nvidia 1080tiprint(tf.test.is_gpu_availab…

Identify if there are two of the same character adjacent to eachother

Ive been asked to create a program that identifies if a password is valid or not. The one part I am struggling with is identifying whether there are two of the same character adjacent to each other. He…

Extract lined table from scanned document opencv python

I want to extract the information from a scanned table and store it a csv. Right now my table extraction algorithm does the following steps.Apply skew correction Apply a gaussian filter for denoising. …

Nested Python C Extensions/Modules?

How do I compile a C-Python module such that it is local to another? E.g. if I have a module named "bar" and another module named "mymodule", how do I compile "bar" so th…

ImportError: No module named sysconfig--cant get pip working

Im really struggling with pip on a RedHat 6.9 system. Every time I tried to use pip, I got ImportError: No module named sysconfigI tried Googling for solutions. I dont have apt-get and cant seem to get…

Convert Dataframe to a Dictionary with List Values

Suppose I have a Dataframe df :Label1 Label2 Label3 key1 col1value1 col2value1 key2 col1value2 col2value2 key3 col1value3 col2value3dict1 = df.set_index(Label1).to_dic…

Efficiently count all the combinations of numbers having a sum close to 0

I have following pandas dataframe df column1 column2 list_numbers sublist_column x y [10,-6,1,-4] a b [1,3,7,-2] p q [6,2,-3,-3.…