linux openpty 例子,Python termios.PARENB屬性代碼示例

# 需要導入模塊: 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)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值