termios
— POSIX style tty controlPOSIX样式tty控件¶
This module provides an interface to the POSIX calls for tty I/O control. 该模块为tty I/O控制的POSIX调用提供了一个接口。For a complete description of these calls, see termios(3) Unix manual page. 有关这些调用的完整描述,请参阅termios(3)Unix手册页。It is only available for those Unix versions that support POSIX termios style tty I/O control configured during installation.它只适用于那些在安装过程中配置的支持POSIX termios风格tty I/O控制的Unix版本。
All functions in this module take a file descriptor fd as their first argument. 该模块中的所有函数都将文件描述符fd作为它们的第一个参数。This can be an integer file descriptor, such as returned by 这可以是一个整数文件描述符,例如由sys.stdin.fileno()
, or a file object, such as sys.stdin
itself.sys.stdin.fileno()
返回的描述符,也可以是文件对象,例如sys.stdin
本身。
This module also defines all the constants needed to work with the functions provided here; these have the same name as their counterparts in C. 该模块还定义了使用此处提供的函数所需的所有常量;这些与C中的对应名称相同。Please refer to your system documentation for more information on using these terminal control interfaces.有关使用这些终端控制接口的更多信息,请参阅您的系统文档。
The module defines the following functions:该模块定义了以下功能:
-
termios.
tcgetattr
(fd)¶ Return a list containing the tty attributes for file descriptor fd, as follows:返回一个包含文件描述符fd的tty属性的列表,如下所示:[iflag, oflag, cflag, lflag, ispeed, ospeed, cc]
where cc is a list of the tty special characters (each a string of length 1, except the items with indicesVMIN
andVTIME
, which are integers when these fields are defined).[iflag, oflag, cflag, lflag, ispeed, ospeed, cc]
其中cc是tty特殊字符的列表(每个都是长度为1的字符串,但索引为VMIN和VTIME的项除外,当定义这些字段时,它们是整数)。The interpretation of the flags and the speeds as well as the indexing in the cc array must be done using the symbolic constants defined in the标志和速度的解释以及cc数组中的索引必须使用termios
module.termios
模块中定义的符号常量来完成。
-
termios.
tcsetattr
(fd, when, attributes)¶ Set the tty attributes for file descriptor fd from the attributes, which is a list like the one returned by从属性中设置文件描述符fd的tty属性,该列表类似于tcgetattr()
.tcgetattr()
返回的列表。The when argument determines when the attributes are changed:when参数确定属性何时更改:TCSANOW
to change immediately,TCSADRAIN
to change after transmitting all queued output, orTCSAFLUSH
to change after transmitting all queued output and discarding all queued input.TCSANOW
立即更改,TCSADRAIN
在传输所有排队输出后更改,或TCSAFLUSH
在传输所有队列输出并丢弃所有排队输入后更改。
-
termios.
tcsendbreak
(fd, duration)¶ Send a break on file descriptor fd.发送文件描述符fd的中断。A zero duration sends a break for 0.25–0.5 seconds; a nonzero duration has a system dependent meaning.零duration发送0.250.5秒的中断;非零duration具有与系统相关的含义。
-
termios.
tcdrain
(fd)¶ Wait until all output written to file descriptor fd has been transmitted.等待,直到写入文件描述符fd的所有输出都已传输。
-
termios.
tcflush
(fd, queue)¶ Discard queued data on file descriptor fd.放弃文件描述符fd上的排队数据。The queue selector specifies which queue:queue选择器指定哪个队列:TCIFLUSH
for the input queue,TCOFLUSH
for the output queue, orTCIOFLUSH
for both queues.TCIFLUSH
用于输入队列,TCOFLUSH
用于输出队列,或TCIOFLUSH
用于两个队列。
-
termios.
tcflow
(fd, action)¶ Suspend or resume input or output on file descriptor fd.挂起或恢复文件描述符fd上的输入或输出。The action argument can beaction参数可以是用于挂起输出的TCOOFF
to suspend output,TCOON
to restart output,TCIOFF
to suspend input, orTCION
to restart input.TCOOFF
、用于重新启动输出的TCOON
、用于挂起输入的TCIOFF
或用于重新启动输入的TCION
。
See also
Module模块tty
Convenience functions for common terminal control operations.通用终端控制操作的便利功能。
Example示例¶
Here’s a function that prompts for a password with echoing turned off. 这里有一个功能,可以在关闭回声的情况下提示输入密码。Note the technique using a separate 请注意使用单独的tcgetattr()
call and a try
… finally
statement to ensure that the old tty attributes are restored exactly no matter what happens: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