termios
--- POSIX 風格 tty 控制¶
This module provides an interface to the POSIX calls for tty I/O control. For acomplete description of these calls, seetermios(3) Unix manualpage. It is only available for those Unix versions that support POSIXtermios style tty I/O control configured during installation.
適用: Unix.
All functions in this module take a file descriptorfd as their firstargument. This can be an integer file descriptor, such as returned bysys.stdin.fileno()
, or afile object, such assys.stdin
itself.
This module also defines all the constants needed to work with the functionsprovided here; these have the same name as their counterparts in C. Pleaserefer to your system documentation for more information on using these terminalcontrol interfaces.
The module defines the following functions:
- termios.tcgetattr(fd)¶
Return a list containing the tty attributes for file descriptorfd, asfollows:
[iflag,oflag,cflag,lflag,ispeed,ospeed,cc]
wherecc is alist of the tty special characters (each a string of length 1, except theitems with indicesVMIN
andVTIME
, which are integers whenthese fields are defined). The interpretation of the flags and the speeds aswell as the indexing in thecc array must be done using the symbolicconstants defined in thetermios
module.
- termios.tcsetattr(fd,when,attributes)¶
Set the tty attributes for file descriptorfd from theattributes, which isa list like the one returned by
tcgetattr()
. Thewhen argumentdetermines when the attributes are changed:- termios.TCSANOW¶
Change attributes immediately.
- termios.TCSADRAIN¶
Change attributes after transmitting all queued output.
- termios.TCSAFLUSH¶
Change attributes after transmitting all queued output anddiscarding all queued input.
- termios.tcsendbreak(fd,duration)¶
Send a break on file descriptorfd. A zeroduration sends a break for0.25--0.5 seconds; a nonzeroduration has a system dependent meaning.
- termios.tcdrain(fd)¶
Wait until all output written to file descriptorfd has been transmitted.
- termios.tcflush(fd,queue)¶
Discard queued data on file descriptorfd. Thequeue selector specifieswhich queue:
TCIFLUSH
for the input queue,TCOFLUSH
for theoutput queue, orTCIOFLUSH
for both queues.
- termios.tcflow(fd,action)¶
Suspend or resume input or output on file descriptorfd. Theactionargument can be
TCOOFF
to suspend output,TCOON
to restartoutput,TCIOFF
to suspend input, orTCION
to restart input.
- termios.tcgetwinsize(fd)¶
Return a tuple
(ws_row,ws_col)
containing the tty window size for filedescriptorfd. Requirestermios.TIOCGWINSZ
ortermios.TIOCGSIZE
.在 3.11 版被加入.
- termios.tcsetwinsize(fd,winsize)¶
Set the tty window size for file descriptorfd fromwinsize, which isa two-item tuple
(ws_row,ws_col)
like the one returned bytcgetwinsize()
. Requires at least one of the pairs(termios.TIOCGWINSZ
,termios.TIOCSWINSZ
);(termios.TIOCGSIZE
,termios.TIOCSSIZE
) to be defined.在 3.11 版被加入.
也參考
tty
模組Convenience functions for common terminal control operations.
範例¶
Here's a function that prompts for a password with echoing turned off. Note thetechnique using a separatetcgetattr()
call and atry
...finally
statement to ensure that the old tty attributes are restoredexactly no matter what happens:
defgetpass(prompt="Password: "):importtermios,sysfd=sys.stdin.fileno()old=termios.tcgetattr(fd)new=termios.tcgetattr(fd)new[3]=new[3]&~termios.ECHO# lflagstry:termios.tcsetattr(fd,termios.TCSADRAIN,new)passwd=input(prompt)finally:termios.tcsetattr(fd,termios.TCSADRAIN,old)returnpasswd