我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用tty.setraw()。
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 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 spawn(argv, master_read=_read, stdin_read=_read): """Create a spawned process.""" if type(argv) == type(''): argv = (argv,) pid, master_fd = fork() if pid == CHILD: os.execlp(argv[0], *argv) try: mode = tty.tcgetattr(STDIN_FILENO) tty.setraw(STDIN_FILENO) restore = 1 except tty.error: # This is the same as termios.error restore = 0 try: _copy(master_fd, master_read, stdin_read) except (IOError, OSError): if restore: tty.tcsetattr(STDIN_FILENO, tty.TCSAFLUSH, mode) os.close(master_fd)
def execCommand(self, proto, cmd): from twisted.internet import reactor uid, gid = self.avatar.getUserGroupId() homeDir = self.avatar.getHomeDir() shell = self.avatar.getShell() or '/bin/sh' command = (shell, '-c', cmd) peer = self.avatar.conn.transport.transport.getPeer() host = self.avatar.conn.transport.transport.getHost() self.environ['SSH_CLIENT'] = '%s %s %s' % (peer.host, peer.port, host.port) if self.ptyTuple: self.getPtyOwnership() self.pty = reactor.spawnProcess(proto, \ shell, command, self.environ, homeDir, uid, gid, usePTY = self.ptyTuple or 0) if self.ptyTuple: self.addUTMPEntry() if self.modes: self.setModes() # else: # tty.setraw(self.pty.pipes[0].fileno(), tty.TCSANOW) self.avatar.conn.transport.transport.setTcpNoDelay(1)
def _handles(stdin, stdout, stderr): master = None if stdout is PTY: # Normally we could just use subprocess.PIPE and be happy. # Unfortunately, this results in undesired behavior when # printf() and similar functions buffer data instead of # sending it directly. # # By opening a PTY for STDOUT, the libc routines will not # buffer any data on STDOUT. master, slave = pty.openpty() # By making STDOUT a PTY, the OS will attempt to interpret # terminal control codes. We don't want this, we want all # input passed exactly and perfectly to the process. tty.setraw(master) tty.setraw(slave) # Pick one side of the pty to pass to the child stdout = slave return stdin, stdout, stderr, master
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 main (): signal.signal (signal.SIGCHLD, signal_handler) pid, fd = pty.fork() if pid == 0: os.write (sys.stdout.fileno(), 'This is a test.\nThis is a test.') time.sleep(10000) nonblock (fd) tty.setraw(fd) #STDIN_FILENO) print 'Sending SIGKILL to child pid:', pid time.sleep(2) os.kill (pid, signal.SIGKILL) print 'Entering to sleep...' try: time.sleep(2) except: print 'Sleep interrupted' try: os.kill(pid, 0) print '\tChild is alive. This is ambiguous because it may be a Zombie.' except OSError as e: print '\tChild appears to be dead.' # print str(e) print print 'Reading from master fd:', os.read (fd, 1000)
def spawn(argv, master_read=_read, stdin_read=_read): """Create a spawned process.""" if type(argv) == type(''): argv = (argv,) pid, master_fd = fork() if pid == CHILD: os.execlp(argv[0], *argv) try: mode = tty.tcgetattr(STDIN_FILENO) tty.setraw(STDIN_FILENO) restore = 1 except tty.error: # This is the same as termios.error restore = 0 try: _copy(master_fd, master_read, stdin_read) except OSError: if restore: tty.tcsetattr(STDIN_FILENO, tty.TCSAFLUSH, mode) os.close(master_fd)
def spawn(argv, master_read=_read, stdin_read=_read): """Create a spawned process.""" if type(argv) == type(''): argv = (argv,) pid, master_fd = fork() if pid == CHILD: os.execlp(argv[0], *argv) try: mode = tty.tcgetattr(STDIN_FILENO) tty.setraw(STDIN_FILENO) restore = 1 except tty.error: # This is the same as termios.error restore = 0 try: _copy(master_fd, master_read, stdin_read) except OSError: if restore: tty.tcsetattr(STDIN_FILENO, tty.TCSAFLUSH, mode) os.close(master_fd) return os.waitpid(pid, 0)[1]
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'