getpass --- ورودی قابل‌حمل گذرواژه

کد منبع: Lib/getpass.py


دسترس‌پذیری: not WASI.

این ماژول روی WebAssembly کار نمی‌کند یا در دسترس نیست. برای اطلاعات بیشتر سکوهای WebAssembly را ببینید.

ماژول getpass دو تابع ارائه می‌دهد:

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).

آرگومان echo_char نحوه‌ی نمایش ورودی کاربر هنگام تایپ را کنترل می‌کند. اگر echo_char برابر None (پیش‌فرض) باشد، ورودی مخفی می‌ماند. در غیر این صورت، echo_char باید یک نویسه‌ی ASCII قابل‌چاپ واحد باشد و هر نویسه‌ی تایپ‌شده با آن جایگزین می‌شود. برای مثال، echo_char='*' به‌جای ورودی واقعی، ستاره‌ها را نمایش می‌دهد.

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: پارامتر echo_char برای بازخورد صفحه‌کلید افزوده شد.

تغییر یافته در نسخه‌ی 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 پرتاب می‌شود.

به‌طور کلی، این تابع باید به os.getlogin() ترجیح داده شود.

تغییر یافته در نسخه‌ی 3.13: پیش‌تر، استثناهای مختلفی علاوه بر OSError نیز پرتاب می‌شدند.