15.10. getpass — 便携式密码输入工具

getpass 模块提供了两个函数:

getpass.getpass([prompt[, stream]])

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. stream defaults to the controlling terminal (/dev/tty) or if that is unavailable to sys.stderr (this argument is ignored on Windows).

如果回显自由输入不可用则 getpass() 将回退为打印一条警告消息到 stream 并且从 sys.stdin 读取同时发出 GetPassWarning。

在 2.5 版更改: The stream parameter was added.

在 2.6 版更改: On Unix it defaults to using /dev/tty before falling back to sys.stdin and sys.stderr.

注解

如果你从 IDLE 内部调用 getpass,输入可能是在你启动 IDLE 的终端中而非在 IDEL 窗口本身中完成。

exception getpass.GetPassWarning

一个当密码输入可能被回显时发出的 UserWarning 子类。

getpass.getuser()

返回用户的“登录名称”。

此函数会按顺序检查环境变量 LOGNAME, USER, LNAME 和 USERNAME,并返回其中第一个被设置为非空字符串的值。 如果均未设置,则在支持 pwd 模块的系统上将返回来自密码数据库的登录名,否则将引发一个异常。