簡體   English   中英

如何使用python腳本從htpasswd文件訪問用戶名的值

[英]How to access the value of username from htpasswd file using python script

我已經在htpasswd文件中創建了一個用戶名/密碼。 在我的python腳本文件中,我想訪問用戶名值以進行進一步處理。 我該如何實現? 我正在使用Linux OS。

htpasswd格式為`:“,解析起來並不難,只需逐行並在冒號上分割,取第一個值即可:

usernames = []
with open("passwdfile") as htpwd:
    for line in htpwd.readlines():
        username, pwd = line.split(":")
        usernames.append(username)

或更簡潔:

with open("passwdfile") as htpwd:
    usernames = [line.split(":")[0] for line in htpwd.readlines()]

暫無
暫無

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

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