getpass --- 可搬性のあるパスワード入力機構

ソースコード: Lib/getpass.py


Availability: not WASI.

このモジュールは WebAssembly では動作しないか、利用不可です。詳しくは、WebAssembly プラットフォーム を見てください。

The getpass module provides two functions:

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

Prompt the user for a password without echoing. The user is prompted using the string prompt, which defaults to 'Password: '. On Unix, the prompt is written to the file-like object stream using the replace error handler if needed. stream defaults to the controlling terminal (/dev/tty) or if that is unavailable to sys.stderr (this argument is ignored on Windows).

The echo_char argument controls how user input is displayed while typing. If echo_char is None (default), input remains hidden. Otherwise, echo_char must be a single printable ASCII character and each typed character is replaced by it. For example, echo_char='*' will display asterisks instead of the actual input.

If echo-free input is unavailable, getpass() falls back to printing a warning message to stream and reading from sys.stdin and issuing a GetPassWarning.

注釈

If you call getpass() from within IDLE, the input may be done in the terminal you launched IDLE from rather than the idle window itself.

注釈

On Unix systems, when echo_char is set, the terminal will be configured to operate in noncanonical mode. Common terminal control characters are supported:

  • Ctrl+A - Move cursor to beginning of line

  • Ctrl+E - Move cursor to end of line

  • Ctrl+K - Kill (delete) from cursor to end of line

  • Ctrl+U - Kill (delete) entire line

  • Ctrl+W - Erase previous word

  • Ctrl+V - Insert next character literally (quote)

  • Backspace/DEL - Delete character before cursor

These shortcuts work by reading the terminal's configured control character mappings from termios settings.

バージョン 3.14 で変更: Added the echo_char parameter for keyboard feedback.

バージョン 3.15 で変更: When using non-empty echo_char on Unix, keyboard shortcuts (including cursor movement and line editing) are now properly handled using the terminal's control character configuration.

exception getpass.GetPassWarning

UserWarning のサブクラスで、入力がエコーされてしまった場合に発生します。

getpass.getuser()

ユーザーの "ログイン名"を返します。

この関数は環境変数 LOGNAME USER LNAME USERNAME の順序でチェックして、最初の空ではない文字列が設定された値を返します。もし、なにも設定されていない場合は pwd モジュールが提供するシステム上のパスワードデータベースから返します。それ以外の場合は、 OSError が送出されます。

In general, this function should be preferred over os.getlogin().

バージョン 3.13 で変更: 以前は OSError だけでなく、様々な例外が送出されていました。