本文整理匯總了Python中termios.tcsetattr方法的典型用法代碼示例。如果您正苦於以下問題:Python termios.tcsetattr方法的具體用法?Python termios.tcsetattr怎麽用?Python termios.tcsetattr使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類termios
的用法示例。
在下文中一共展示了termios.tcsetattr方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: raw_terminal
# 需要導入模塊: import termios [as 別名]
# 或者: from termios import tcsetattr [as 別名]
def raw_terminal():
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)
yield fd
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
sys.stdout.flush()
if f is not None:
f.close()
except termios.error:
pass
示例2: getchar
# 需要導入模塊: import termios [as 別名]
# 或者: from termios import tcsetattr [as 別名]
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')
示例3: read_char_no_blocking
# 需要導入模塊: import termios [as 別名]
# 或者: from termios import tcsetattr [as 別名]
def read_char_no_blocking():
''' Read a character in nonblocking mode, if no characters are present in the buffer, return an empty string '''
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
old_flags = fcntl.fcntl(fd, fcntl.F_GETFL)
try:
tty.setraw(fd, termios.TCSADRAIN)
fcntl.fcntl(fd, fcntl.F_SETFL, old_flags | os.O_NONBLOCK)
return sys.stdin.read(1)
except IOError as e:
ErrorNumber = e[0]
# IOError with ErrorNumber 11(35 in Mac) is thrown when there is nothing to read(Resource temporarily unavailable)
if (sys.platform.startswith("linux") and ErrorNumber != 11) or (sys.platform == "darwin" and ErrorNumber != 35):
raise
return ""
finally:
fcntl.fcntl(fd, fcntl.F_SETFL, old_flags)
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
示例4: restore_echo
# 需要導入模塊: import termios [as 別名]
# 或者: from termios import tcsetattr [as 別名]
def restore_echo():
"""Hack for https://stackoverflow.com/questions/6488275 equivalent
issue with Nmap (from
http://stackoverflow.com/a/8758047/3223422)
"""
try:
fdesc = sys.stdin.fileno()
except ValueError:
return
try:
attrs = termios.tcgetattr(fdesc)
except termios.error:
return
attrs[3] = attrs[3] | termios.ECHO
termios.tcsetattr(fdesc, termios.TCSADRAIN, attrs)
示例5: __init__
# 需要導入模塊: import termios [as 別名]
# 或者: from termios import tcsetattr [as 別名]
def __init__(self, verbosity, maxJobs):
super().__init__(verbosity)
self.__index = 1
self.__maxJobs = maxJobs
self.__jobs = {}
self.__slots = [None] * maxJobs
self.__tasksDone = 0
self.__tasksNum = 1
# disable cursor
print("\x1b[?25l")
# disable echo
try:
import termios
fd = sys.stdin.fileno()
self.__oldTcAttr = termios.tcgetattr(fd)
new = termios.tcgetattr(fd)
new[3] = new[3] & ~termios.ECHO
termios.tcsetattr(fd, termios.TCSADRAIN, new)
except ImportError:
pass
示例6: __init__
# 需要導入模塊: import termios [as 別名]
# 或者: from termios import tcsetattr [as 別名]
def __init__(self, args, close_stderr=False):
pid, fd = pty.fork()
if pid == 0:
# We're the child. Transfer control to command.
if close_stderr:
dev_null = os.open('/dev/null', 0)
os.dup2(dev_null, 2)
os.execvp(args[0], args)
else:
# Disable echoing.
attr = termios.tcgetattr(fd)
attr[3] = attr[3] & ~termios.ECHO
termios.tcsetattr(fd, termios.TCSANOW, attr)
# Set up a file()-like interface to the child process
self.r = os.fdopen(fd, 'r', 1)
self.w = os.fdopen(os.dup(fd), 'w', 1)
示例7: init
# 需要導入模塊: import termios [as 別名]
# 或者: from termios import tcsetattr [as 別名]
def init():
if os.name == 'posix':
global old
old = termios.tcgetattr(fd)
new = termios.tcgetattr(fd)
new[3] = new[3] & ~termios.ICANON & ~termios.ECHO
new[6][termios.VMIN] = 1
new[6][termios.VTIME] = 0
# -- Cambiar los atributos
termios.tcsetattr(fd, termios.TCSANOW, new)
# -- Restaurar el terminal a la salida
atexit.register(cleanup_console)
pass
# -------------------------------------
# Pequena prueba del modulo
# -------------------------------------
示例8: _set_baudrate
# 需要導入模塊: import termios [as 別名]
# 或者: from termios import tcsetattr [as 別名]
def _set_baudrate(self, baudrate):
if not isinstance(baudrate, int):
raise TypeError("Invalid baud rate type, should be integer.")
if baudrate not in Serial._BAUDRATE_TO_OSPEED:
raise ValueError("Unknown baud rate: {:d}".format(baudrate))
# Get tty attributes
try:
(iflag, oflag, cflag, lflag, ispeed, ospeed, cc) = termios.tcgetattr(self._fd)
except termios.error as e:
raise SerialError(e.errno, "Getting serial port attributes: " + e.strerror)
# Modify tty attributes
cflag &= ~(termios.CBAUD | termios.CBAUDEX)
cflag |= Serial._BAUDRATE_TO_OSPEED[baudrate]
ispeed = Serial._BAUDRATE_TO_OSPEED[baudrate]
ospeed = Serial._BAUDRATE_TO_OSPEED[baudrate]
# Set tty attributes
try:
termios.tcsetattr(self._fd, termios.TCSANOW, [iflag, oflag, cflag, lflag, ispeed, ospeed, cc])
except termios.error as e:
raise SerialError(e.errno, "Setting serial port attributes: " + e.strerror)
示例9: _set_databits
# 需要導入模塊: import termios [as 別名]
# 或者: from termios import tcsetattr [as 別名]
def _set_databits(self, databits):
if not isinstance(databits, int):
raise TypeError("Invalid data bits type, should be integer.")
elif databits not in [5, 6, 7, 8]:
raise ValueError("Invalid data bits, can be 5, 6, 7, 8.")
# Get tty attributes
try:
(iflag, oflag, cflag, lflag, ispeed, ospeed, cc) = termios.tcgetattr(self._fd)
except termios.error as e:
raise SerialError(e.errno, "Getting serial port attributes: " + e.strerror)
# Modify tty attributes
cflag &= ~termios.CSIZE
cflag |= Serial._DATABITS_TO_CFLAG[databits]
# Set tty attributes
try:
termios.tcsetattr(self._fd, termios.TCSANOW, [iflag, oflag, cflag, lflag, ispeed, ospeed, cc])
except termios.error as e:
raise SerialError(e.errno, "Setting serial port attributes: " + e.strerror)
示例10: _set_stopbits
# 需要導入模塊: import termios [as 別名]
# 或者: from termios import tcsetattr [as 別名]
def _set_stopbits(self, stopbits):
if not isinstance(stopbits, int):
raise TypeError("Invalid stop bits type, should be integer.")
elif stopbits not in [1, 2]:
raise ValueError("Invalid stop bits, can be 1, 2.")
# Get tty attributes
try:
(iflag, oflag, cflag, lflag, ispeed, ospeed, cc) = termios.tcgetattr(self._fd)
except termios.error as e:
raise SerialError(e.errno, "Getting serial port attributes: " + e.strerror)
# Modify tty attributes
cflag &= ~termios.CSTOPB
if stopbits == 2:
cflag |= termios.CSTOPB
# Set tty attributes
try:
termios.tcsetattr(self._fd, termios.TCSANOW, [iflag, oflag, cflag, lflag, ispeed, ospeed, cc])
except termios.error as e:
raise SerialError(e.errno, "Setting serial port attributes: " + e.strerror)
示例11: _set_xonxoff
# 需要導入模塊: import termios [as 別名]
# 或者: from termios import tcsetattr [as 別名]
def _set_xonxoff(self, enabled):
if not isinstance(enabled, bool):
raise TypeError("Invalid enabled type, should be boolean.")
# Get tty attributes
try:
(iflag, oflag, cflag, lflag, ispeed, ospeed, cc) = termios.tcgetattr(self._fd)
except termios.error as e:
raise SerialError(e.errno, "Getting serial port attributes: " + e.strerror)
# Modify tty attributes
iflag &= ~(termios.IXON | termios.IXOFF | termios.IXANY)
if enabled:
iflag |= (termios.IXON | termios.IXOFF)
# Set tty attributes
try:
termios.tcsetattr(self._fd, termios.TCSANOW, [iflag, oflag, cflag, lflag, ispeed, ospeed, cc])
except termios.error as e:
raise SerialError(e.errno, "Setting serial port attributes: " + e.strerror)
示例12: _set_vmin
# 需要導入模塊: import termios [as 別名]
# 或者: from termios import tcsetattr [as 別名]
def _set_vmin(self, vmin):
if not isinstance(vmin, int):
raise TypeError("Invalid vmin type, should be integer.")
elif not (0 <= vmin <= 255):
raise ValueError("Invalid vmin, can be 0 to 255.")
try:
iflag, oflag, cflag, lflag, ispeed, ospeed, cc = termios.tcgetattr(self._fd)
except termios.error as e:
raise SerialError(e.errno, "Setting serial port attributes: " + e.strerror)
cc[termios.VMIN] = vmin
try:
termios.tcsetattr(self._fd, termios.TCSANOW, [iflag, oflag, cflag, lflag, ispeed, ospeed, cc])
except termios.error as e:
raise SerialError(e.errno, "Setting serial port attributes: " + e.strerror)
self._use_termios_timeout = vmin > 0
示例13: _set_vtime
# 需要導入模塊: import termios [as 別名]
# 或者: from termios import tcsetattr [as 別名]
def _set_vtime(self, vtime):
if not isinstance(vtime, (float, int)):
raise TypeError("Invalid vtime type, should be float or integer.")
elif not (0 <= vtime <= 25.5):
raise ValueError("Invalid vtime, can be 0 to 25.5 seconds.")
try:
iflag, oflag, cflag, lflag, ispeed, ospeed, cc = termios.tcgetattr(self._fd)
except termios.error as e:
raise SerialError(e.errno, "Setting serial port attributes: " + e.strerror)
cc[termios.VTIME] = int(float(vtime) * 10.0)
try:
termios.tcsetattr(self._fd, termios.TCSANOW, [iflag, oflag, cflag, lflag, ispeed, ospeed, cc])
except termios.error as e:
raise SerialError(e.errno, "Setting serial port attributes: " + e.strerror)
示例14: launch_proc_with_pty
# 需要導入模塊: import termios [as 別名]
# 或者: from termios import tcsetattr [as 別名]
def launch_proc_with_pty(args, stdin=None, stdout=None, stderr=None, echo=True):
"""Similar to pty.fork, but handle stdin/stdout according to parameters
instead of connecting to the pty
:return tuple (subprocess.Popen, pty_master)
"""
def set_ctty(ctty_fd, master_fd):
os.setsid()
os.close(master_fd)
fcntl.ioctl(ctty_fd, termios.TIOCSCTTY, 0)
if not echo:
termios_p = termios.tcgetattr(ctty_fd)
# termios_p.c_lflags
termios_p[3] &= ~termios.ECHO
termios.tcsetattr(ctty_fd, termios.TCSANOW, termios_p)
(pty_master, pty_slave) = os.openpty()
# pylint: disable=not-an-iterable
p = yield from asyncio.create_subprocess_exec(*args,
stdin=stdin,
stdout=stdout,
stderr=stderr,
preexec_fn=lambda: set_ctty(pty_slave, pty_master))
os.close(pty_slave)
return p, open(pty_master, 'wb+', buffering=0)
示例15: _setecho
# 需要導入模塊: import termios [as 別名]
# 或者: from termios import tcsetattr [as 別名]
def _setecho(fd, state):
errmsg = 'setecho() may not be called on this platform (it may still be possible to enable/disable echo when spawning the child process)'
try:
attr = termios.tcgetattr(fd)
except termios.error as err:
if err.args[0] == errno.EINVAL:
raise IOError(err.args[0], '%s: %s.' % (err.args[1], errmsg))
raise
if state:
attr[3] = attr[3] | termios.ECHO
else:
attr[3] = attr[3] & ~termios.ECHO
try:
# I tried TCSADRAIN and TCSAFLUSH, but these were inconsistent and
# blocked on some platforms. TCSADRAIN would probably be ideal.
termios.tcsetattr(fd, termios.TCSANOW, attr)
except IOError as err:
if err.args[0] == errno.EINVAL:
raise IOError(err.args[0], '%s: %s.' % (err.args[1], errmsg))
raise