Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
POSIX Style TTY control using Python
The termios module provides an interface to the POSIX calls for TTY I/O control. It is only available for Unix-based systems and allows you to control terminal behavior programmatically.
Importing the Module
To use the termios module, import it as follows ?
import termios
All methods in this module take a file descriptor as an argument. The module provides several methods for controlling terminal attributes.
Method termios.tcgetattr(fd)
This method returns a list of TTY attributes for the given file descriptor. The attributes include:
- iflag ? Input mode flags
- oflag ? Output mode flags
- cflag ? Control mode flags
- lflag ? Local mode flags
- ispeed ? Input speed
- ospeed ? Output speed
- cc ? Control characters
Method termios.tcsetattr(fd, when, attributes)
This method sets the terminal attributes from a list of attributes. The when parameter determines when the attributes will be changed ?
| Constant | Meaning |
|---|---|
| TCSANOW | Change attributes immediately |
| TCSADRAIN | Change attributes after transmitting all queued output |
| TCSAFLUSH | Change attributes after transmitting all queued output and discard all queued inputs |
Method termios.tcsendbreak(fd, duration)
This method sends a break signal on the file descriptor. When the duration is zero, it sends a break for 0.25-0.5 seconds.
Method termios.tcdrain(fd)
This method waits until all output written to the file descriptor has been transmitted.
Method termios.tcflush(fd, queue)
This method discards queued data on the file descriptor. The queue selector specifies which queue to flush ?
- TCIFLUSH ? Flush input queue
- TCOFLUSH ? Flush output queue
- TCIOFLUSH ? Flush both input and output queues
Example: Password Input Without Echo
Here's a practical example that disables terminal echo to create a secure password input function ?
import termios
import sys
def get_password(prompt="Enter Password: "):
file_desc = sys.stdin.fileno()
old_attrs = termios.tcgetattr(file_desc)
new_attrs = termios.tcgetattr(file_desc)
# Disable echo by turning off the ECHO flag
new_attrs[3] &= ~termios.ECHO
try:
termios.tcsetattr(file_desc, termios.TCSADRAIN, new_attrs)
password = input(prompt)
finally:
# Restore original terminal settings
termios.tcsetattr(file_desc, termios.TCSADRAIN, old_attrs)
return password
# Usage
password = get_password()
print(f"\nEntered Password: {password}")
The output shows the prompt but doesn't display the typed characters ?
$ python3 example.py Enter Password: Entered Password: my_password
How It Works
The example works by:
- Getting the current terminal attributes using
tcgetattr() - Creating a copy and disabling the ECHO flag
- Setting the new attributes with
tcsetattr() - Reading the password input (which won't be displayed)
- Restoring the original terminal settings in the finally block
Conclusion
The termios module provides low-level control over terminal behavior in Unix systems. It's particularly useful for creating secure input functions and custom terminal interfaces that require specific I/O control settings.
