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 の文字列をプロンプトに使え、デフォルトは 'Password: ' です。 Unixではプロンプトはファイルに似たオブジェクト stream へ、必要なら置き換えられたエラーハンドラを使って出力されます。 stream のデフォルトは、制御端末(/dev/tty)か、それが利用できない場合は sys.stderr です (この引数は 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.

もしエコーなしで入力が利用できない場合は、 getpass()stream に警告メッセージを出力し、 sys.stdin から読み込み、 GetPassWarning 警告を発生させます。

注釈

IDLE から getpass を呼び出した場合、入力はIDLEのウィンドウではなく、IDLE を起動したターミナルから行われます。

注釈

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 だけでなく、様々な例外が送出されていました。