getpass --- 可攜式密碼輸入工具

原始碼:Lib/getpass.py


Availability: not Emscripten, not WASI.

This module does not work or is not available on WebAssembly platforms wasm32-emscripten and wasm32-wasi. See WebAssembly 平台 for more information.

getpass 模組 (module) 提供了兩個函式:

getpass.getpass(prompt='Password: ', stream=None)

提示使用者輸入一個密碼且不會有回音 (echo)。使用者會看到字串 prompt 作為提示,其預設值為 'Password: '。在 Unix 上,如有必要的話會使用替換錯誤處理函式 (replace error handler) 寫入到類檔案物件 (file-like object) stream中。stream 預設為主控終端機 (controlling terminal) (/dev/tty),如果不可用則為 sys.stderr (此引數在 Windows 上會被忽略)。

如果無回音輸入 (echo-free input) 無法使用則 getpass() 將回退為印出一條警告訊息到 stream,並從 sys.stdin 讀取且同時發出 GetPassWarning

備註

如果你從 IDLE 內部呼叫 getpass,輸入可能會在你啟動 IDLE 的終端機中完成,而非在 IDLE 視窗中。

exception getpass.GetPassWarning

當密碼輸入可能被回音時會發出的 UserWarning 子類別。

getpass.getuser()

回傳使用者的"登入名稱"。

This function checks the environment variables LOGNAME, USER, LNAME and USERNAME, in order, and returns the value of the first one which is set to a non-empty string. If none are set, the login name from the password database is returned on systems which support the pwd module, otherwise, an exception is raised.

大部分情況下,此函式應該要比 os.getlogin() 優先使用。