getpass --- 便携式密码输入工具

源代码: Lib/getpass.py


Availability: not WASI.

This module does not work or is not available on WebAssembly. See WebAssembly 平台 for more information.

getpass 模块提供了两个函数:

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

提示用户输入一个密码且不会回显。 用户会看到字符串 prompt 作为提示,其默认值为 'Password: '。 在 Unix 上,如有必要提示会使用替换错误句柄写入到文件型对象 streamstream 默认指向控制终端 (/dev/tty),如果不可用则指向 sys.stderr (此参数在 Windows 上会被忽略)。

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

备注

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

exception getpass.GetPassWarning

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

getpass.getuser()

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

This function checks the environment variables LOGNAME, USER, LNAME and USERNAME, in order, and returns the value of the first one which is set to a non-empty string. If none are set, the login name from the password database is returned on systems which support the pwd module, otherwise, an OSError is raised.

通常情况下,此函数应优先于 os.getlogin() 使用。

在 3.13 版本发生变更: Previously, various exceptions beyond just OSError were raised.