zoukankan      html  css  js  c++  java
  • 分分钟提升命令行模式下密码输入逼格

    简介

      今天突然想到一个事,linux-termial  下一个优秀的程序在输入密码的时候整个窗口是看不到密码的明文的;我前一段时间总是用input来接收输入的信息,

      明显没有逼格啊!而且我坚信 python 中一定已经有这个轮子了。

    三行代码成就有逼格的密码输入

      总的来说就是用getpass模块的getpass函数(和input的用法一样哈!)

    In [1]: import getpass                                                          
    
    In [2]: password = getpass.getpass("user password:")                            
    user password:
    
    In [3]: password                                                                
    Out[3]: '12345678'

    源码的关键部分

      可以看到 getpass 和 input 的低层实现都是 sys.stdin 

    def _raw_input(prompt="", stream=None, input=None):
        # This doesn't save the string in the GNU readline history.
        if not stream:
            stream = sys.stderr
        if not input:
            input = sys.stdin
        prompt = 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 EOFError
        if line[-1] == '
    ':
            line = line[:-1]
        return line
    
    
    
    def 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 to
                  the 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 = None
        with 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 = input
            except OSError as e:
                # If that fails, see if stdin can be controlled.
                stack.close()
                try:
                    fd = sys.stdin.fileno()
                except (AttributeError, ValueError):
                    fd = None
                    passwd = fallback_getpass(prompt, stream)
                input = sys.stdin
                if not stream:
                    stream = sys.stderr
    
            if fd is not None:
                try:
                    old = termios.tcgetattr(fd)     # a copy to save
                    new = old[:]
                    new[3] &= ~termios.ECHO  # 3 == 'lflags'
                    tcsetattr_flags = termios.TCSAFLUSH
                    if hasattr(termios, 'TCSASOFT'):
                        tcsetattr_flags |= termios.TCSASOFT
                    try:
                        termios.tcsetattr(fd, tcsetattr_flags, new)
                        passwd = _raw_input(prompt, stream, input=input)
                    finally:
                        termios.tcsetattr(fd, tcsetattr_flags, old)
                        stream.flush()  # issue7208
                except 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 blocking
                        stack.close()
                    passwd = fallback_getpass(prompt, stream)
    
            stream.write('
    ')
            return passwd
    
    
    try:
        import termios
        # it's possible there is an incompatible termios from the
        # McMillan Installer, make sure we have a UNIX-compatible termios
        termios.tcgetattr, termios.tcsetattr
    except (ImportError, AttributeError):
        try:
            import msvcrt
        except ImportError:
            getpass = fallback_getpass
        else:
            getpass = win_getpass
    else:
        getpass = unix_getpass

      https://www.sqlpy.com

    ----

  • 相关阅读:
    用C#制作PDF文件全攻略
    侦测软件鸟哥linux学习笔记之源代码安装侦测软件
    类模式Java设计模式之十五(桥接模式)类模式
    安装配置Maven入门什么是maven和maven的安装和配置安装配置
    查看表空间oracle rman catalog目录数据库创建过程查看表空间
    产品群体互联网产品设计规划产品群体
    问题修改highcharts 导出图片 .net c#(二)问题修改
    音频播放android4.2音频管理技术音频播放
    重启启动eclipse 中启动Tomcat超时了错误如下:重启启动
    关系建立对于内向、不善于社交的人来说,如何建立人脉?关系建立
  • 原文地址:https://www.cnblogs.com/JiangLe/p/10347743.html
Copyright © 2011-2022 走看看