簡體   English   中英

如何在python中使用htpasswd連接當前用戶和Web應用程序的用戶?

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

我有一個使用htpasswd身份驗證的python腳本。 我希望根據當前登錄的用戶來修改腳本。 此應用程序已啟用Web。 例如。

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

任何用戶都可以隨時/同時登錄。 想要最簡單,最有效的解決方案。 有人可以幫忙嗎? 我如何知道哪個用戶已在任何時間登錄? 在腳本中導入getpass getpass.getuser()

如果要檢索特定用戶名的值,則可以使用字典存儲用戶名和值之間的映射。 例如,上面的代碼可以被重寫:

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是一個不錯的起點)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM