我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用termios.TCSADRAIN。
def getch(): """ Interrupting program until pressed any key """ try: import msvcrt return msvcrt.getch() except ImportError: import sys import tty import 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
def __init__(self, slave=0, pid=os.getpid()): # apparently python GC's modules before class instances so, here # we have some hax to ensure we can restore the terminal state. self.termios, self.fcntl = termios, fcntl # open our controlling PTY self.pty = open(os.readlink("/proc/%d/fd/%d" % (pid, slave)), "rb+") # store our old termios settings so we can restore after # we are finished self.oldtermios = termios.tcgetattr(self.pty) # get the current settings se we can modify them newattr = termios.tcgetattr(self.pty) # set the terminal to uncanonical mode and turn off # input echo. newattr[3] &= ~termios.ICANON & ~termios.ECHO # don't handle ^C / ^Z / ^\ newattr[6][termios.VINTR] = '\x00' newattr[6][termios.VQUIT] = '\x00' newattr[6][termios.VSUSP] = '\x00' # set our new attributes termios.tcsetattr(self.pty, termios.TCSADRAIN, newattr) # store the old fcntl flags self.oldflags = fcntl.fcntl(self.pty, fcntl.F_GETFL) # fcntl.fcntl(self.pty, fcntl.F_SETFD, fcntl.FD_CLOEXEC) # make the PTY non-blocking fcntl.fcntl(self.pty, fcntl.F_SETFL, self.oldflags | os.O_NONBLOCK)
def getchar(echo): if not isatty(sys.stdin): f = open('/dev/tty') fd = f.fileno() else: fd = sys.stdin.fileno() f = None try: old_settings = termios.tcgetattr(fd) try: tty.setraw(fd) ch = os.read(fd, 32) if echo and isatty(sys.stdout): sys.stdout.write(ch) finally: termios.tcsetattr(fd, termios.TCSADRAIN, old_settings) sys.stdout.flush() if f is not None: f.close() except termios.error: pass _translate_ch_to_exc(ch) return ch.decode(get_best_encoding(sys.stdin), 'replace')
def _getch(self): '''Getch a character from stdin''' fd = sys.stdin.fileno() old_settings = termios.tcgetattr(fd) ch = None while self._state: try: tty.setraw(fd) ch = sys.stdin.read(1) finally: termios.tcsetattr(fd, termios.TCSADRAIN, old_settings) if ch: if ch == 'q': self._state = False self.manageEvent('exit') else: self.manageEvent(ch)
def calibrate(self): """Detect keylogger file lines format. """ term_settings = termios.tcgetattr(sys.stdin) tty.setraw(sys.stdin) with open(self.filepath, 'r') as keylog: keylog.seek(0, 2) print(config.TERM.clear + '[1/3] Press <Enter>', end='') sys.stdin.read(1) print(config.TERM.clear + '[2/3] Press <Ctrl>+R', end='') sys.stdin.read(1) termios.tcsetattr(sys.stdin, termios.TCSADRAIN, term_settings) print(config.TERM.clear + '[3/3] Calibration done') time.sleep(2) line = keylog.readlines()[-1].upper() pattern = r'(<(%s)>)R' % '|'.join(MODIFIERS) try: config.CONFIG['chars_skip'] = re.search(pattern, line).start() except AttributeError: logger.error('Cannot detect key logger line format. ' + "Check '%s' content." % self.filepath) exit(1) pattern = r'(<(%s)>)R<\2>' % '|'.join(MODIFIERS) config.CONFIG['format_closing'] = re.search(pattern, line) is not None config.save()
def _find_getch(): try: import termios except ImportError: import msvcrt return msvcrt.getch import sys, tty def _getch(): fd = sys.stdin.fileno() old_settings = termios.tcgetattr(fd) try: tty.setraw(fd) ch = sys.stdin.read(1) finally: termios.tcsetattr(fd, termios.TCSADRAIN, old_settings) return ch return _getch # read single character without waiting for '\n'
def __call__(self, timeout): import sys, tty, termios from select import select ch = None fd = sys.stdin.fileno() old_settings = termios.tcgetattr(fd) try: tty.setraw(sys.stdin.fileno()) rlist, _, _ = select([sys.stdin], [], [], timeout) if rlist: ch = ord(sys.stdin.read(1)) finally: termios.tcsetattr(fd, termios.TCSADRAIN, old_settings) return ch