本文整理汇总了Python中tty.tcgetattr函数的典型用法代码示例。如果您正苦于以下问题:Python tcgetattr函数的具体用法?Python tcgetattr怎么用?Python tcgetattr使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了tcgetattr函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: setup_tty_for_pty
def setup_tty_for_pty(func):
"""
Sets up tty for raw mode while retaining original tty settings and then
starts the reactor to connect to the pty. Upon exiting pty, restores
original tty settings.
:param func:
The callable to run after the tty is ready, such as ``reactor.run``
"""
# Preserve original tty settings
stdin_fileno = sys.stdin.fileno()
old_ttyattr = tty.tcgetattr(stdin_fileno)
try:
# Enter raw mode on the local tty.
tty.setraw(stdin_fileno)
raw_ta = tty.tcgetattr(stdin_fileno)
raw_ta[tty.LFLAG] |= tty.ISIG
raw_ta[tty.OFLAG] |= tty.OPOST | tty.ONLCR
# Pass ^C through so we can abort traceroute, etc.
raw_ta[tty.CC][tty.VINTR] = '\x18' # ^X is the new ^C
# Ctrl-Z is used by a lot of vendors to exit config mode
raw_ta[tty.CC][tty.VSUSP] = 0 # disable ^Z
tty.tcsetattr(stdin_fileno, tty.TCSANOW, raw_ta)
# Execute our callable here
func()
finally:
# Restore original tty settings
tty.tcsetattr(stdin_fileno, tty.TCSANOW, old_ttyattr)
示例2: setup
def setup(self):
if tty:
self.tcattr = tty.tcgetattr(sys.stdin.fileno())
tcattr = tty.tcgetattr(sys.stdin.fileno())
tcattr[0] = tcattr[0] & ~(tty.IXON)
tty.tcsetattr(sys.stdin.fileno(), tty.TCSANOW, tcattr)
self.w = curses.initscr()
curses.cbreak()
curses.noecho()
try: curses.meta(1)
except: pass
self.cursor(0)
signal.signal(signal.SIGCHLD, signal.SIG_IGN)
signal.signal(signal.SIGHUP, self.handler_quit)
signal.signal(signal.SIGINT, self.handler_quit)
signal.signal(signal.SIGTERM, self.handler_quit)
signal.signal(signal.SIGWINCH, self.handler_resize)
self.win_root = RootWindow(None)
self.win_root.update()
self.win_status = self.win_root.win_status
self.status = self.win_status.status
self.win_podlist = self.win_root.win_tab.win_podlist
self.timeout = Timeout()
示例3: join_term
def join_term(self):
out('Successfully joined %s' % (self.room_url()))
self.orig_stdout_atts = tty.tcgetattr(sys.stdout)
stdout = sys.stdout.fileno()
tty.setraw(stdout)
fl = fcntl.fcntl(stdout, fcntl.F_GETFL)
fcntl.fcntl(stdout, fcntl.F_SETFL, fl | os.O_NONBLOCK)
self.orig_stdin_atts = tty.tcgetattr(sys.stdin)
stdin = sys.stdin.fileno()
tty.setraw(stdin)
fl = fcntl.fcntl(stdin, fcntl.F_GETFL)
fcntl.fcntl(stdin, fcntl.F_SETFL, fl | os.O_NONBLOCK)
def ship_stdin(fd):
data = read(fd)
if data:
self.transport("term_stdin", {'data': data, 'id': self.term_id})
if 'term_stdin' in self.ri['perms']:
out('You have permission to write to this terminal. Remember: With great power comes great responsibility.')
self.add_fd(stdin, reader=ship_stdin, name='join_term_stdin')
else:
out('You do not have permission to write to this terminal.')
def stdout_write(buf):
write(stdout, buf.encode('utf-8'))
self.handle_stdio = stdout_write
self._set_pty_size(self.ri['terms'][str(self.term_id)]['size'])
示例4: __init__
def __init__(self, filename):
SerialIO.__init__(self, filename)
self.fd = os.open(filename, os.O_RDWR | os.O_NOCTTY | os.O_NONBLOCK)
tty.setraw(self.fd)
attr = tty.tcgetattr(self.fd)
attr[tty.ISPEED] = attr[tty.OSPEED] = tty.B57600
tty.tcsetattr(self.fd, tty.TCSAFLUSH, attr)
示例5: __enter__
def __enter__(self):
try:
self.mode = tty.tcgetattr(self.fd)
tty.setraw(self.fd)
self.restore = True
except tty.error: # This is the same as termios.error
pass
示例6: pty_spawn
def pty_spawn(argv):
"""Version of pty.spawn() for PY2, that returns the exit code.
This works around https://bugs.python.org/issue2489.
"""
logger.info("Using builtin pty.spawn()")
import pty
import tty
if isinstance(argv, bytes):
argv = (argv,)
pid, master_fd = pty.fork()
if pid == pty.CHILD:
os.execlp(argv[0], *argv)
try:
mode = tty.tcgetattr(pty.STDIN_FILENO)
tty.setraw(pty.STDIN_FILENO)
restore = 1
except tty.error: # This is the same as termios.error
restore = 0
try:
pty._copy(master_fd, pty._read, pty._read)
except (IOError, OSError):
if restore:
tty.tcsetattr(pty.STDIN_FILENO, tty.TCSAFLUSH, mode)
os.close(master_fd)
return os.waitpid(pid, 0)[1]
示例7: posix_shell
def posix_shell(chan,logfile):
import select
oldtty = termios.tcgetattr(sys.stdin)
f=open(logfile,'w')
try:
mode = tty.tcgetattr(sys.stdin.fileno())
tty.setraw(sys.stdin.fileno())
tty.setcbreak(sys.stdin.fileno())
chan.settimeout(0.0)
while True:
r, w, e = select.select([chan, sys.stdin], [], [])
if chan in r:
try:
x = chan.recv(1024)
if len(x) == 0:
sys.stdout.write('\r\n*** EOF\r\n')
break
sys.stdout.write(x)
sys.stdout.flush()
f.write(x)
f.flush()
except socket.timeout:
pass
if sys.stdin in r:
#x = sys.stdin.read(1)
x=os.read(sys.stdin.fileno(),1000)
if len(x) == 0:
break
chan.send(x)
finally:
termios.tcsetattr(sys.stdin, termios.TCSADRAIN, oldtty)
f.close()
示例8: setModes
def setModes(self):
pty = self.pty
attr = tty.tcgetattr(pty.fileno())
for mode, modeValue in self.modes:
if mode not in ttymodes.TTYMODES:
continue
ttyMode = ttymodes.TTYMODES[mode]
if len(ttyMode) == 2: # Flag.
flag, ttyAttr = ttyMode
if not hasattr(tty, ttyAttr):
continue
ttyval = getattr(tty, ttyAttr)
if modeValue:
attr[flag] = attr[flag] | ttyval
else:
attr[flag] = attr[flag] & ~ttyval
elif ttyMode == 'OSPEED':
attr[tty.OSPEED] = getattr(tty, 'B%s' % (modeValue,))
elif ttyMode == 'ISPEED':
attr[tty.ISPEED] = getattr(tty, 'B%s' % (modeValue,))
else:
if not hasattr(tty, ttyMode):
continue
ttyval = getattr(tty, ttyMode)
attr[tty.CC][ttyval] = chr(modeValue)
tty.tcsetattr(pty.fileno(), tty.TCSANOW, attr)
示例9: spawn
def spawn(argv, master_read=pty._read, stdin_read=pty._read, handle_window_size=False):
# copied from pty.py, with modifications
# note that it references a few private functions - would be nice to not
# do that, but you know
if type(argv) == type(''):
argv = (argv,)
pid, master_fd, slave_name = fork(handle_window_size)
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
if handle_window_size:
signal.signal(
signal.SIGWINCH,
lambda signum, frame: _winch(slave_name, pid)
)
while True:
try:
pty._copy(master_fd, master_read, stdin_read)
except OSError as e:
if e.errno == errno.EINTR:
continue
if restore:
tty.tcsetattr(STDIN_FILENO, tty.TCSAFLUSH, mode)
break
os.close(master_fd)
return os.waitpid(pid, 0)[1]
示例10: spawn
def spawn(self, argv=None):
'''
Create a spawned process.
Based on the code for pty.spawn().
'''
assert self.master_fd is None
if not argv:
argv = [os.environ['SHELL']]
pid, master_fd = pty.fork()
self.master_fd = master_fd
if pid == pty.CHILD:
os.execlp(argv[0], *argv)
old_handler = signal.signal(signal.SIGWINCH, self._signal_winch)
try:
mode = tty.tcgetattr(pty.STDIN_FILENO)
tty.setraw(pty.STDIN_FILENO)
restore = 1
except tty.error: # This is the same as termios.error
restore = 0
self._init_fd()
try:
self._copy()
except (IOError, OSError):
if restore:
tty.tcsetattr(pty.STDIN_FILENO, tty.TCSAFLUSH, mode)
os.close(master_fd)
self.master_fd = None
signal.signal(signal.SIGWINCH, old_handler)
self._set_pty_size()
示例11: _connectSSHLocal
def _connectSSHLocal(self, hostname, username, passwd):
sshcmd = [self.ssh_bin, '%[email protected]%s' % (username, hostname)]
if self.ssh_extraopt:
sshcmd += self.ssh_extraopt.split()
self.addStringToClipboard(passwd)
pid, self.remote_fd = pty.fork()
if pid == pty.CHILD:
os.execlp(sshcmd[0], *sshcmd)
try:
mode = tty.tcgetattr(pty.STDIN_FILENO)
tty.setraw(pty.STDIN_FILENO)
restore = True
except tty.error:
restore = False
signal.signal(signal.SIGWINCH, self._winchHandler)
self._setRemoteTTYSize(self.remote_fd)
self._setTerminalTitle('%[email protected]%s' % (username, hostname))
try:
self._copySSHData(self.remote_fd, passwd)
except (IOError, OSError):
pass
except:
if restore:
tty.tcsetattr(pty.STDIN_FILENO, tty.TCSAFLUSH, mode)
raise
if restore:
tty.tcsetattr(pty.STDIN_FILENO, tty.TCSAFLUSH, mode)
signal.signal(signal.SIGWINCH, signal.SIG_DFL)
os.close(self.remote_fd)
示例12: _spawn
def _spawn(self):
'''Create a spawned process.
Based on pty.spawn() from standard library.
'''
assert self.master_fd is None
pid, self.master_fd = pty.fork()
if pid == pty.CHILD:
os.execlp(self.command[0], *self.command)
old_handler = signal.signal(signal.SIGWINCH, self._signal_winch)
try:
mode = tty.tcgetattr(pty.STDIN_FILENO)
tty.setraw(pty.STDIN_FILENO)
restore = 1
except tty.error: # This is the same as termios.error
restore = 0
self._set_pty_size()
try:
self._copy()
except (IOError, OSError):
if restore:
tty.tcsetattr(pty.STDIN_FILENO, tty.TCSAFLUSH, mode)
os.close(self.master_fd)
self.master_fd = None
signal.signal(signal.SIGWINCH, old_handler)
return True
示例13: _spawn
def _spawn(shell, master_read):
"""Create a spawned process.
Modified version of pty.spawn with terminal size support.
"""
pid, master_fd = pty.fork()
if pid == pty.CHILD:
os.execlp(shell, shell)
try:
mode = tty.tcgetattr(pty.STDIN_FILENO)
tty.setraw(pty.STDIN_FILENO)
restore = True
except tty.error: # This is the same as termios.error
restore = False
_set_pty_size(master_fd)
signal.signal(signal.SIGWINCH, lambda *_: _set_pty_size(master_fd))
try:
pty._copy(master_fd, master_read, pty._read)
except OSError:
if restore:
tty.tcsetattr(pty.STDIN_FILENO, tty.TCSAFLUSH, mode)
os.close(master_fd)
return os.waitpid(pid, 0)[1]
示例14: spawn
def spawn(argv, master_read=pty_read, stdin_read=pty_read):
"""Create a spawned process.
Based on pty.spawn code."""
# TODO(larsbutler): This type check won't work with python3
# See http://packages.python.org/six/#six.string_types
# for a possible solution.
if isinstance(argv, (basestring)):
argv = (argv,)
pid, master_fd = pty.fork()
if pid == pty.CHILD:
os.execlp(argv[0], *argv)
try:
mode = tty.tcgetattr(pty.STDIN_FILENO)
tty.setraw(pty.STDIN_FILENO)
restore = 1
except tty.error: # This is the same as termios.error
restore = 0
# get pseudo-terminal window size
buf = array.array('h', [0, 0, 0, 0])
fcntl.ioctl(pty.STDOUT_FILENO, termios.TIOCGWINSZ, buf, True)
# pass window size settings to forked one
fcntl.ioctl(master_fd, termios.TIOCSWINSZ, buf)
try:
pty_copy(master_fd, master_read, stdin_read)
except (IOError, OSError):
if restore:
tty.tcsetattr(pty.STDIN_FILENO, tty.TCSAFLUSH, mode)
os.close(master_fd)
示例15: read_chr
def read_chr():
old_settings = tty.tcgetattr(sys.stdin.fileno())
tty.setraw(sys.stdin, tty.TCSANOW)
chr = sys.stdin.read(1)
tty.tcsetattr(sys.stdin.fileno(), tty.TCSADRAIN, old_settings)
return chr