termios - POSIX风格的tty控制

平台: Unix

该模块提供了一个用于tty I / O控制的POSIX调用的接口。有关这些调用的完整说明,请参见POSIX或Unix手册页。它仅适用于那些支持POSIX termios风格的tty I / O控制的Unix版本(然后只有在安装时配置)。

该模块中的所有函数都将文件描述符fd作为其第一个参数。这可以是整数文件描述符,如sys.stdin.fileno()返回的 文件描述符,也可以是文件对象,如sys.stdin本身。

该模块还定义了使用此处提供的功能所需的所有常量; 这些名称与C中的对应名称相同。有关使用这些终端控制接口的更多信息,请参阅系统文档。

该模块定义了以下功能:

termios的。tcgetattr fd 
返回包含文件描述符fd的tty属性的列表,如下所示:iflag oflag, cflag, lflag, ispeed, ospeed, cc]其中cc是tty特殊字符的列表(每个长度为1的字符串,索引为VMINVTIME的项目,这些项目在定义这些字段时是整数)。必须使用termios模块中定义的符号常量来完成cc数组中标志和速度的解释以及索引
termios的。tcsetattr fdwhenattributes 
为属性设置文件描述符fd的tty属性,这些属性tcgetattr()返回属性列表参数确定时的属性被更改:TCSANOW立即改变,TCSADRAIN于发送所有排队的输出之后改变,或者TCSAFLUSH于发送所有排队的输出和废弃所有排队的输入后发生变化。
termios的。tcsendbreak fdduration 
发送文件描述符fd中断持续时间发送一个中断0.25 -0.5秒; 非零持续时间具有系统依赖性意义。
termios的。tcdrain fd 
等到写入文件描述符fd的所有输出都被发送完毕。
termios的。tcflush fdqueue 
丢弃文件描述符fd上的排队数据队列选择器指定哪些队列:TCIFLUSH用于输入队列,TCOFLUSH为输出队列,或TCIOFLUSH两个队列。
termios的。tcflow fdaction 
在文件描述符fd上挂起或恢复输入或输出操作 参数可以是TCOOFF暂停输出,TCOON重启输出,TCIOFF暂停输入,或TCION重启输入。

也可以看看

模块tty
常见终端控制操作的便捷功能。
示例¶
这里有一个函数,提示输入关闭回显的密码。注意使用单独的tcgetattr()调用和try ... finally语句的技巧,以确保无论发生什么情况,旧的tty属性都可以完全恢复:

def getpass(prompt = "Password: "):
    import termios, sys
    fd = sys.stdin.fileno()
    old = termios.tcgetattr(fd)
    new = termios.tcgetattr(fd)
    new[3] = new[3] & ~termios.ECHO          # lflags
    try:
        termios.tcsetattr(fd, termios.TCSADRAIN, new)
        passwd = input(prompt)
    finally:
        termios.tcsetattr(fd, termios.TCSADRAIN, old)
    return passwd

 

for python 3.x

复制代码
[root@master test]# /usr/local/python3.4/bin/python3 test.py 
Enter your password:
***your password is 123
[root@master test]# cat test.py 
#!/usr/bin/python
# -*- coding=utf-8 -*-

import sys, tty, termios 

#for python 3.x
def getch():  
  fd = sys.stdin.fileno() 
  old_settings = termios.tcgetattr(fd) 
  try: 
    tty.setraw(sys.stdin.fileno()) 
    ch = sys.stdin.read(1) 
  finally: 
    termios.tcsetattr(fd, termios.TCSADRAIN, old_settings) 
  return ch 
def getpass(maskchar = "*"): 
  password = "" 
  while True: 
    ch = getch() 
    if ch == "\r" or ch == "\n": 
      print 
      return password 
    elif ch == "\b" or ord(ch) == 127: 
      if len(password) > 0: 
        sys.stdout.write("\b \b") 
        password = password[:-1] 
    else: 
      if maskchar != None: 
        sys.stdout.write(maskchar) 
      password += ch 
if __name__ == "__main__": 
  print ("Enter your password:",)
  password = getpass("*") 
  print ("your password is %s" %password)
复制代码
Note:这种方法可以实现输入显示星号,而且还有退格功能,该方法仅在Linux上使用。

四、msvcrt.getch()

复制代码
F:\Python\Alex\s12\zhulh>python test.py
Please input your password:
***
your password is:123

#!/usr/bin/python
# -*- coding=utf-8 -*-
import msvcrt,sys
def pwd_input():    
    chars = []   
    while True:  
        try:  
            newChar = msvcrt.getch().decode(encoding="utf-8")  
        except:  
            return input("你很可能不是在cmd命令行下运行,密码输入将不能隐藏:")  
        if newChar in '\r\n': # 如果是换行,则输入结束               
             break   
        elif newChar == '\b': # 如果是退格,则删除密码末尾一位并且删除一个星号   
             if chars:    
                 del chars[-1]   
                 msvcrt.putch('\b'.encode(encoding='utf-8')) # 光标回退一格  
                 msvcrt.putch( ' '.encode(encoding='utf-8')) # 输出一个空格覆盖原来的星号  
                 msvcrt.putch('\b'.encode(encoding='utf-8')) # 光标回退一格准备接受新的输入                   
        else:  
            chars.append(newChar)  
            msvcrt.putch('*'.encode(encoding='utf-8')) # 显示为星号  
    return (''.join(chars) )  
  
print("Please input your password:")
pwd = pwd_input()  
print("\nyour password is:{0}".format(pwd))
sys.exit()
复制代码
Note:这种方法可以实现输入显示星号,而且还有退格功能,该方法仅在Windows上使用。

 

在这里提供shell实现的输入密码显示星号的方法:

复制代码
[root@master test]# sh ./passwd.sh 
Please input your passwd: ***
Your password is: 123
[root@master test]# cat passwd.sh 
#!/bin/sh

getchar() {
    stty cbreak -echo
    dd if=/dev/tty bs=1 count=1 2> /dev/null
    stty -cbreak echo
}

printf "Please input your passwd: "

while : ; do
    ret=`getchar`
    if [ x$ret =  x ]; then
        echo
        break
    fi
    str="$str$ret"
    printf "*"
done
echo "Your password is: $str"
复制代码

这里还有一个获取跨平台按键的例子:

复制代码
class _Getch:  
    """Gets a single character from standard input.  Does not echo to the screen."""  
    def __init__(self):  
        try:  
            self.impl = _GetchWindows()  
        except ImportError:  
            try:  
                self.impl = _GetchMacCarbon()  
            except AttributeError:  
                self.impl = _GetchUnix()  

    def __call__(self): return self.impl()  

class _GetchUnix:  
    def __init__(self):  
        import tty, sys, termios # import termios now or else you'll get the Unix version on the Mac  

    def __call__(self):  
        import sys, tty, termios  
        fd = sys.stdin.fileno()  
        old_settings = termios.tcgetattr(fd)  
        try:  
            tty.setraw(sys.stdin.fileno())  
            ch = sys.stdin.read(1)  
        finally:  
            termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)  
        return ch  

class _GetchWindows:  
    def __init__(self):  
        import msvcrt  

    def __call__(self):  
        import msvcrt  
        return msvcrt.getch()  

class _GetchMacCarbon:  
    """  
    A function which returns the current ASCII key that is down;  
    if no ASCII key is down, the null string is returned.  The  
    page http://www.mactech.com/macintosh-c/chap02-1.html was  
    very helpful in figuring out how to do this.  
    """  
    def __init__(self):  
        import Carbon  
        Carbon.Evt #see if it has this (in Unix, it doesn't)  

    def __call__(self):  
        import Carbon  
        if Carbon.Evt.EventAvail(0x0008)[0]==0: # 0x0008 is the keyDownMask  
            return ''  
        else:  
            #  
            # The event contains the following info:  
            # (what,msg,when,where,mod)=Carbon.Evt.GetNextEvent(0x0008)[1]  
            #  
            # The message (msg) contains the ASCII char which is  
            # extracted with the 0x000000FF charCodeMask; this  
            # number is converted to an ASCII character with chr() and  
            # returned  
            #  
            (what,msg,when,where,mod)=Carbon.Evt.GetNextEvent(0x0008)[1]  
            return chr(msg & 0x000000FF)  

if __name__ == '__main__': # a little test  
   print 'Press a key'  
   inkey = _Getch()  
   import sys  
   for i in xrange(sys.maxint):  
      k=inkey()  
      if k<>'':break  
   print 'you pressed ',k  

 

posted on 2018-03-17 14:09  shisanjun  阅读(611)  评论(0)    收藏  举报

导航