how can I limit the access in Flask

2024/10/10 16:24:30

I create a project to simulate login my company's website.And put it in my server to let others to use.

But the company website has a limit with single ip can only open 2 sessions.

So when more than 2 my colleagues login my project ,the third one can't login.

Is there a way to limit the access , So when there's 2 users are using ,the third one can't login.

Should I use a global variable to store a number of current users?

Answer

You can save the ip address, user id or login and last time of logging in to your app into database and check against it when some user logs in.

If user logs in, you store his/her ip address, id/login and last time of logging in to the app into table. If an attempt is made to login third time from the same ip address you reject it.

When user logs out you remove the record from the database.

You need to be careful with situations when user session expires automatically but the record is in the database.

In order to avoid that you can set permanent session to True (session will not be destroyed even if the browser gets closed) and set its duration to a fixed amount of time - let's say 48 hours. Then you can create a procedure which will run periodically in your db and check the last time of logging in and the duration of the session. If the difference is more than 48 hours, it deletes the record from the database.

You should also take into account whether your ip addresses are static or dynamic. With static ip addresses it should be much easier. With dynamic - it depends on how often they change, but definitely it will be more complicated.

Before a user logs in you can get his/her ip address this way:

from flask import request@app.route('/login', methods=['GET', 'POST']):
def login():ip_address = request.remote_addr# Check the ip_address and how many sessions are bound to it
https://en.xdnf.cn/q/118433.html

Related Q&A

Multiple images numpy array into blocks

I have a numpy array with 1000 RGB images with shape (1000, 90, 90, 3) and I need to work on each image, but sliced in 9 blocks. Ive found many solution for slicing a single image, but how can I obtai…

Python - Transpose columns to rows within data operation and before writing to file

I have developed a public and open source App for Splunk (Nmon performance monitor for Unix and Linux Systems, see https://apps.splunk.com/app/1753/)A master piece of the App is an old perl (recycled, …

Unexpected output while sorting the list of IP address [duplicate]

This question already has answers here:Python .sort() not working as expected(8 answers)Closed last year.I am trying to sort the list of ipaddress from the following list. IPlist= [209.85.238.4, 216.23…

Google Cloud Run returning Server Unavailable Occasionally

I am running a Flask app at https://recycler-mvdcj7favq-uc.a.run.app/ on Google Cloud Run and occasionally I get 503 server unavailable while refreshing a few times seems to load the page. Also, someti…

Connecting to Internet?

Im having issues with connecting to the Internet using python.I am on a corporate network that uses a PAC file to set proxies. Now this would be fine if I could find and parse the PAC to get what I nee…

Angular App Not Working When Moving to Python Flask

Not sure what information to give so will do as much as I can. Currently have an Angular app sitting on IIS and using Classic ASP. All works fine. There is a dropdown which fetches some JSON that then …

How can I subtract tuples in a list?

Lets say I have a list with tuples in it.Something like this:listnum = [(18,12),(12,20)]Is there a way I can subtract what is in the tuples and make listnum into:listnum = [6,8]As you can see It takes …

Chart barh matplotlib - overlap bars

Im new user of matplotlib and I have a problem with chart barh: overlap bars. When plot the graph, the bars draws overlapped and I havent found the reason. In my opinion the problem is on re-size the …

Python django image upload is not working

Python Django image upload is not working when I am adding data and click on submit button not give a any response and also not upload any data in the database.models.py fisrst of all i add model file.…

Android bluetooth send message working first time only

I need to send string message from Raspberry PI to Android device. I am getting message first time only. After that it does not work at all. I am using PYTHON code in Raspberry PI. After first time, it…