本文整理汇总了Python中tty.setraw函数的典型用法代码示例。如果您正苦于以下问题:Python setraw函数的具体用法?Python setraw怎么用?Python setraw使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了setraw函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: interactive
def interactive(self, command, *args, **options):
import termios
import tty
with self._get_chan(options.pop('get_pty', False)) as chan:
with self._forward_agent(chan):
with timeout(self._timeout):
command = self._format_command(command, args, options)
chan.get_pty()
oldtty = termios.tcgetattr(sys.stdin)
try:
tty.setraw(sys.stdin.fileno())
tty.setcbreak(sys.stdin.fileno())
chan.settimeout(0.0)
chan.exec_command(command)
while True:
rlist = select.select([chan, sys.stdin], [], [])[0]
if chan in rlist:
try:
data = self._manage_encoding(chan.recv(1024))
if len(data) == 0:
break
sys.stdout.write(data)
sys.stdout.flush()
except socket.timeout:
pass
if sys.stdin in rlist:
data = sys.stdin.read(1)
if len(data) == 0:
break
chan.send(data)
finally:
termios.tcsetattr(sys.stdin, termios.TCSADRAIN, oldtty)
示例2: main
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)
示例3: 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)
示例4: _posix_shell
def _posix_shell(self, chan):
import select
oldtty = termios.tcgetattr(sys.stdin)
try:
tty.setraw(sys.stdin.fileno())
tty.setcbreak(sys.stdin.fileno())
chan.settimeout(0.0)
# needs to be sent to give vim correct size FIX
chan.send('eval $(resize)\n')
while True:
r, w, e = select.select([chan, sys.stdin], [], [])
if chan in r:
try:
x = chan.recv(1024)
if len(x) == 0:
print '\r\n*** EOF\r\n',
break
sys.stdout.write(x)
sys.stdout.flush()
except socket.timeout:
pass
if sys.stdin in r:
# fixes up arrow problem
x = os.read(sys.stdin.fileno(), 1)
if len(x) == 0:
break
chan.send(x)
finally:
termios.tcsetattr(sys.stdin, termios.TCSADRAIN, oldtty)
示例5: getch
def getch():
"Returns a single character"
if getch.platform is None:
try:
# Window's python?
import msvcrt
getch.platform = 'windows'
except ImportError:
# Fallback...
try:
import tty, termios
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
getch.platform = 'unix'
except termios.error:
getch.platform = 'dumb'
if getch.platform == 'windows':
import msvcrt
return msvcrt.getch()
elif getch.platform == 'unix':
import tty, 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
else:
return sys.stdin.read(1).strip().lower()
示例6: interactive
def interactive(self, prompt = pwn.text.boldred('$') + ' '):
'''Turns the channel into an interactive session (that is, it connects stdin and stdout to the channel).'''
import sys, tty, termios, select
if not self._tty:
basechatter.interactive(self, prompt)
return
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
tty.setraw(fd)
try:
while True:
reads, _, _ = select.select([sys.stdin.fileno(), self._channel.fileno()], [], [], 0.05)
while self._channel.recv_ready():
dat = self.recv()
sys.stdout.write(dat)
sys.stdout.flush()
if self._channel.exit_status_ready():
if not self._channel.recv_ready():
break
elif sys.stdin.fileno() in reads:
dat = sys.stdin.read(1)
# Break if ctrl+] is hit
if dat == '\x1d':
sys.stdout.write('\r\n')
sys.stdout.flush()
break
self.send(dat)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
示例7: getch
def getch():
"""getch() for Windows and Linux.
This won't work unless run from a terminal.
"""
# this must be executed from a terminal
if not is_executed_from_console():
system_exit("libastyle.getch() must be run from the console")
# WINDOWS uses msvcrt
if os.name == "nt":
# clear buffer
while msvcrt.kbhit():
msvcrt.getch()
# read char
ch_in = msvcrt.getch()
# LINUX uses termios and tty
else:
# clear buffer
sys.stdin.flush()
# read char
fd_in = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd_in)
try:
tty.setraw(sys.stdin.fileno())
ch_in = sys.stdin.read(1)
if ch_in == '\x1b': # alt key (27)
ch_in = sys.stdin.read(1)
finally:
termios.tcsetattr(fd_in, termios.TCSADRAIN, old_settings)
# convert to unicode for Python 3
return ch_in.decode('utf-8')
示例8: myinput
def myinput(timeout=0):
ESCSEQ = b'\x1b['
tty.setraw(stdin.fileno())
#stdout = os.fdopen(stdin.fileno(), 'wb', 0)
special = False
while True:
rlist, wlist, xlist = select([stdin], [], [], 0)
ch = os.read(stdin.fileno(), 1)
ch = ch.decode()
if ch == '\x1b':
if special:
yield ESC
else:
special = True
elif ch == '[':
if not special:
yield ch
else:
if special:
special = False
if ch == 'A': yield UP
elif ch == 'B': yield DOWN
elif ch == 'C': yield RIGHT
elif ch == 'D': yield LEFT
else:
yield ch
示例9: geolocation
def geolocation():
sys.stdout.write('\x1b[?99n')
sys.stdout.flush()
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(sys.stdin.fileno())
rv = sys.stdin.read(1)
if rv != '\x1b':
raise
rv = sys.stdin.read(1)
if rv != '[':
raise
rv = sys.stdin.read(1)
if rv != '?':
raise
loc = ''
while rv != 'R':
rv = sys.stdin.read(1)
if rv != 'R':
loc += rv
except Exception:
return
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
if not loc or ';' not in loc:
return
return tuple(map(float, loc.split(';')))
示例10: interactive_shell
def interactive_shell(self, chan):
sys.stdout.flush()
try:
signal.signal(signal.SIGHUP, self._session.kill_session)
oldtty = termios.tcgetattr(sys.stdin)
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:
break
sys.stdout.write(x)
sys.stdout.flush()
except socket.timeout:
break
if sys.stdin in r:
x = os.read(sys.stdin.fileno(), 1)
if len(x) == 0:
break
chan.send(x)
termios.tcsetattr(sys.stdin, termios.TCSADRAIN, oldtty)
except Exception as e:
logger.error(e.message)
raise e
示例11: 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()
示例12: interact
def interact(self):
os.write(self.fd, self.readbuffer)
old = termios.tcgetattr(self.fd)
new = termios.tcgetattr(self.fd)
new[3] = new[3] & ~termios.ECHO
try:
tty.setraw(self.fd)
while True:
try:
rd, wd, ed = select.select([self.ptyfd, self.fd], [], [])
except select.error as e:
if e.args[0] == 4:
continue
else:
raise
for i in rd:
if i == self.ptyfd:
s = os.read(i, 1024)
os.write(self.fd, s)
elif i == self.fd:
s = os.read(i, 1024)
os.write(self.ptyfd, s)
except OSError as e:
if e.errno == 5:
# 使用 print() 会导致下一个 Python 提示符位置不对
os.write(self.fd, '已结束。\r\n'.encode())
else:
raise
finally:
termios.tcsetattr(self.fd, termios.TCSADRAIN, old)
示例13: Connect
def Connect(self):
self.file = open_rfcomm(self.port, os.O_RDWR)
tty.setraw(self.file)
attrs = termios.tcgetattr(self.file)
attrs[0] &= ~(termios.IGNCR | termios.ICRNL | termios.IUCLC | termios.INPCK | termios.IXON | termios.IXANY |
termios.IGNPAR)
attrs[1] &= ~(termios.OPOST | termios.OLCUC | termios.OCRNL | termios.ONLCR | termios.ONLRET)
attrs[3] &= ~(termios.ICANON | getattr(termios, 'XCASE', 4) | termios.ECHO | termios.ECHOE | termios.ECHONL)
attrs[3] &= ~(termios.ECHO | termios.ECHOE)
attrs[6][termios.VMIN] = 1
attrs[6][termios.VTIME] = 0
attrs[6][termios.VEOF] = 1
attrs[2] &= ~(termios.CBAUD | termios.CSIZE | termios.CSTOPB | termios.CLOCAL | termios.PARENB)
attrs[2] |= (termios.B9600 | termios.CS8 | termios.CREAD | termios.PARENB)
termios.tcsetattr(self.file, termios.TCSANOW, attrs)
termios.tcflush(self.file, termios.TCIOFLUSH)
self.send_commands()
示例14: Init
def Init():
global stdout_fd
global stdin_fd
stdout_fd = os.fdopen(sys.stdout.fileno(), 'w', 0)
stdin_fd = os.fdopen(sys.stdin.fileno(), 'r', 0)
tty.setraw(stdin_fd)
示例15: interactive_shell
def interactive_shell(chan):
"""stolen from paramiko examples"""
oldtty = termios.tcgetattr(sys.stdin)
try:
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:
break
sys.stdout.write(x)
sys.stdout.flush()
except socket.timeout:
pass
if sys.stdin in r:
x = sys.stdin.read(1)
if len(x) == 0:
break
chan.send(x)
finally:
termios.tcsetattr(sys.stdin, termios.TCSADRAIN, oldtty)