# 需要導入模塊: import termios [as 別名]
# 或者: from termios import PARENB [as 別名]
def __init__(self, printer, prot, iomanager):
self.printer = printer
self.prot = prot
self.iomanager = iomanager
(master_fd, slave_fd) = os.openpty()
slave = os.ttyname(slave_fd)
master_flags = fcntl.fcntl(master_fd, fcntl.F_GETFL, 0)
fcntl.fcntl(master_fd, fcntl.F_SETFL, master_flags | os.O_NONBLOCK)
# switch to "raw" mode - these constants come from the manpage for termios under cfmakeraw()
master_attr = termios.tcgetattr(master_fd)
master_attr[0] &= ~(termios.IGNBRK | termios.BRKINT | termios.PARMRK | termios.ISTRIP
| termios.INLCR | termios.IGNCR | termios.ICRNL | termios.IXON)
master_attr[1] &= ~termios.OPOST
master_attr[2] &= ~(termios.CSIZE | termios.PARENB)
master_attr[3] &= ~(termios.ECHO | termios.ECHONL | termios.ICANON | termios.ISIG
| termios.IEXTEN)
master_attr[3] |= termios.CS8
termios.tcsetattr(master_fd, termios.TCSADRAIN, master_attr)
# Fun detail: master will always show as /dev/ptmx, but the kernel knows from
# the fd which PTY we're using. This means we have to use master_fd instead
# of opening master by name.
logging.info("Opened PTY for {} and got {}".format(prot, os.ttyname(slave_fd)))
self.pipe_link = "/dev/" + prot + "_1"
try:
os.unlink(self.pipe_link)
except OSError as e:
# file not found is fine to ignore - anythine else and we should log it
if e.errno != errno.ENOENT:
logging.error("Failed to unlink '{}': {}".format(self.pipe_link, e.strerror))
logging.info("linking {}".format(self.pipe_link))
os.symlink(slave, self.pipe_link)
os.chmod(self.pipe_link, 0o666)
logging.info("{} Pipe open. Use '{}' to communicate with it".format(self.prot, self.pipe_link))
self.rd = os.fdopen(master_fd, "r")
self.wr = os.fdopen(master_fd, "w")
self.send_response = True
self.iomanager.add_file(self.rd, self.get_message)