0

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 ?

4

1 回答 1

1

如果您要检索特定用户名的值,可以使用字典来存储用户名和值之间的映射。例如,您上面的代码可以重写:

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)

对于更大的系统,您可能希望研究分布式键值存储(redis 将是一个不错的起点)。

于 2012-05-14T08:15:25.897 回答