getpass --- 可搬性のあるパスワード入力機構¶
ソースコード: Lib/getpass.py
Availability: not WASI.
このモジュールは WebAssembly では動作しないか、利用不可です。詳しくは、WebAssembly プラットフォーム を見てください。
getpass モジュールは二つの関数を提供します:
- 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. In particular, this means that line editing shortcuts such as Ctrl+U will not work and may insert unexpected characters into the input.
バージョン 3.14 で変更: Added the echo_char parameter for keyboard feedback.
- exception getpass.GetPassWarning¶
UserWarningのサブクラスで、入力がエコーされてしまった場合に発生します。
- getpass.getuser()¶
ユーザーの "ログイン名"を返します。
この関数は環境変数
LOGNAMEUSERLNAMEUSERNAMEの順序でチェックして、最初の空ではない文字列が設定された値を返します。もし、なにも設定されていない場合はpwdモジュールが提供するシステム上のパスワードデータベースから返します。それ以外の場合は、OSErrorが送出されます。In general, this function should be preferred over
os.getlogin().バージョン 3.13 で変更: 以前は
OSErrorだけでなく、様々な例外が送出されていました。