简体   繁体   中英

How to connect the current user and the user of a web application using htpasswd in python?

I have a python script which is using htpasswd authentication. I wish to modify the script depending on the user who has logged in currently. This application is web-enabled. eg.

if current_username = 'ABC':
   m = 2 
elif current_username = 'PQE':   
   m = 4
else:      
   m = 6

Any of the user can log in any time / simultaneously. Want the simplest and most efficient solution. Can anybody help? How do I know which of the user has logged in at any point of time? import getpass getpass.getuser() in the script ?

If you are looking to retrieve a value for specific usernames, you could use a dictionary to store the mapping between username and value. For example, your code above could be rewritten:

username_mapping = {"ABC": 2, "PQE": 4}
# The second argument to get will be returned if the key could not be found
# in the dictionary
m = username_mapping.get(current_username, 6)

For a larger system, you might want to look into a distributed key value store (redis would be a good place to start).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM