"""Utilities to get a password and/or the current user name.getpass(prompt[, stream]) - Prompt for a password, with echo turned off.getuser() - Get the user name from the environment or password database.GetPassWarning - This UserWarning is issued when getpass() cannot preventechoing of the password contents while reading.On Windows, the msvcrt module will be used."""# Authors: Piers Lauder (original)# Guido van Rossum (Windows support and cleanup)# Gregory P. Smith (tty support & GetPassWarning)import contextlibimport ioimport osimport sysimport warnings__all__ = ["getpass","getuser","GetPassWarning"]class GetPassWarning(UserWarning): passdef unix_getpass(prompt='Password: ', stream=None):"""Prompt for a password, with echo turned off.Args:prompt: Written on stream to ask for the input. Default: 'Password: 'stream: A writable file object to display the prompt. Defaults tothe tty. If no tty is available defaults to sys.stderr.Returns:The seKr3t input.Raises:EOFError: If our input tty or stdin was closed.GetPassWarning: When we were unable to turn echo off on the input.Always restores terminal settings before returning."""passwd = Nonewith contextlib.ExitStack() as stack:try:# Always try reading and writing directly on the tty first.fd = os.open('/dev/tty', os.O_RDWR|os.O_NOCTTY)tty = io.FileIO(fd, 'w+')stack.enter_context(tty)input = io.TextIOWrapper(tty)stack.enter_context(input)if not stream:stream = inputexcept OSError as e:# If that fails, see if stdin can be controlled.stack.close()try:fd = sys.stdin.fileno()except (AttributeError, ValueError):fd = Nonepasswd = fallback_getpass(prompt, stream)input = sys.stdinif not stream:stream = sys.stderrif fd is not None:try:old = termios.tcgetattr(fd) # a copy to savenew = old[:]new[3] &= ~termios.ECHO # 3 == 'lflags'tcsetattr_flags = termios.TCSAFLUSHif hasattr(termios, 'TCSASOFT'):tcsetattr_flags |= termios.TCSASOFTtry:termios.tcsetattr(fd, tcsetattr_flags, new)passwd = _raw_input(prompt, stream, input=input)finally:termios.tcsetattr(fd, tcsetattr_flags, old)stream.flush() # issue7208except termios.error:if passwd is not None:# _raw_input succeeded. The final tcsetattr failed. Reraise# instead of leaving the terminal in an unknown state.raise# We can't control the tty or stdin. Give up and use normal IO.# fallback_getpass() raises an appropriate warning.if stream is not input:# clean up unused file objects before blockingstack.close()passwd = fallback_getpass(prompt, stream)stream.write('\n')return passwddef win_getpass(prompt='Password: ', stream=None):"""Prompt for password with echo off, using Windows getch()."""if sys.stdin is not sys.__stdin__:return fallback_getpass(prompt, stream)for c in prompt:msvcrt.putwch(c)pw = ""while 1:c = msvcrt.getwch()if c == '\r' or c == '\n':breakif c == '003円':raise KeyboardInterruptif c == '\b':pw = pw[:-1]else:pw = pw + cmsvcrt.putwch('\r')msvcrt.putwch('\n')return pwdef fallback_getpass(prompt='Password: ', stream=None):warnings.warn("Can not control echo on the terminal.", GetPassWarning,stacklevel=2)if not stream:stream = sys.stderrprint("Warning: Password input may be echoed.", file=stream)return _raw_input(prompt, stream)def _raw_input(prompt="", stream=None, input=None):# This doesn't save the string in the GNU readline history.if not stream:stream = sys.stderrif not input:input = sys.stdinprompt = str(prompt)if prompt:try:stream.write(prompt)except UnicodeEncodeError:# Use replace error handler to get as much as possible printed.prompt = prompt.encode(stream.encoding, 'replace')prompt = prompt.decode(stream.encoding)stream.write(prompt)stream.flush()# NOTE: The Python C API calls flockfile() (and unlock) during readline.line = input.readline()if not line:raise EOFErrorif line[-1] == '\n':line = line[:-1]return linedef getuser():"""Get the username from the environment or password database.First try various environment variables, then the passworddatabase. This works on Windows as long as USERNAME is set."""for name in ('LOGNAME', 'USER', 'LNAME', 'USERNAME'):user = os.environ.get(name)if user:return user# If this fails, the exception will "explain" whyimport pwdreturn pwd.getpwuid(os.getuid())[0]# Bind the name getpass to the appropriate functiontry:import termios# it's possible there is an incompatible termios from the# McMillan Installer, make sure we have a UNIX-compatible termiostermios.tcgetattr, termios.tcsetattrexcept (ImportError, AttributeError):try:import msvcrtexcept ImportError:getpass = fallback_getpasselse:getpass = win_getpasselse:getpass = unix_getpass
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。