getpassPortable password input便携式密码输入

Source code: Lib/getpass.py


The getpass module provides two functions:getpass模块提供两个功能:

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

Prompt the user for a password without echoing. 提示用户输入密码而不回显。The user is prompted using the string prompt, which defaults to 'Password: '. 使用字符串prompt提示用户,默认为'Password: 'On Unix, the prompt is written to the file-like object stream using the replace error handler if needed. 在Unix上,如果需要,将使用替换错误处理程序将提示写入类似文件的对象streamstream defaults to the controlling terminal (/dev/tty) or if that is unavailable to sys.stderr (this argument is ignored on Windows).默认为控制终端(/dev/tty),或者如果sys.stderr不可用(在Windows上忽略此参数)。

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.如果无回声输入不可用,getpass()会返回到打印警告消息到流并从sys.stdin读取并发出GetPassWarning

Note

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.如果从IDLE中调用getpass,则输入可能在启动IDLE的终端中完成,而不是在IDLE窗口本身中完成。

exceptiongetpass.GetPassWarning

A UserWarning subclass issued when password input may be echoed.当可能回显密码输入时发出的UserWarning子类。

getpass.getuser()

Return the “login name” of the user.返回用户的“登录名”。

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. 此函数按顺序检查环境变量LOGNAMEUSERLNAMEUSERNAME,并返回设置为非空字符串的第一个变量的值。If none are set, the login name from the password database is returned on systems which support the pwd module, otherwise, an exception is raised.如果未设置,则在支持pwd模块的系统上返回密码数据库中的登录名,否则将引发异常。

In general, this function should be preferred over os.getlogin().通常,此函数应优先于os.getlogin()