【发布时间】:2019-02-19 15:02:04
【问题描述】:
我正在尝试创建一个模拟的 TTY 连接目标设备,我可以通过即 minicom 连接到该设备。我正在使用/dev/ptmx 创建一个 pty 并打印出要由即 minicom 打开的从属名称:
Please connect to: /dev/pts/4。然后在 python 端,我使用 os.read 和 os.write 来做 io 并模拟我的目标:
import os, re, termios
from ctypes import *
class dev():
def __init__(self):
pass
def createpty(self):
self.fd3 = os.open("/dev/ptmx", os.O_RDWR | os.O_NONBLOCK);
if self.fd3 < 0:
print("Couldn't open output /dev/ptmx\n")
libc = cdll.LoadLibrary("libc.so.6")
libc.grantpt(self.fd3);
libc.unlockpt(self.fd3);
libc.ptsname.restype = c_char_p
self.slave = libc.ptsname(self.fd3)
print("Please connect to:" + self.slave);
self.old = termios.tcgetattr(self.fd3)
n = termios.tcgetattr(self.fd3)
n[3] = n[3] & ~(termios.ECHO|termios.ICANON) # c_lflag
n[3] = n[3] & 0
n[4+1] = n[4+1] & 0xffff0000;
termios.tcsetattr(self.fd3, termios.TCSANOW, n)
整个过程相当繁琐。我想在 python 端使用select,这迫使我再次使用 ctypes。我无法使用 os.fdopen 将 self.fd3 包装到文件中,因为我需要防止在 self.fd3 上调用 close。
所以我有两个问题:
- 有谁知道在 python 中实现 pty master 端时处理 pty 创建和操作的现成 python 库?
- 如果没有:是否有示例说明如何通过 ctypes 调用 libc.select?
【问题讨论】:
-
旁注:检查stackoverflow.com/questions/49878901/….. 还有你在哪里使用select?你在说docs.python.org/3/library/select.html吗?
-
我正在尝试通过 ctypes 调用 luv select
-
在哪里?我建议使用 Python 模块(它们都与 fds 一起工作)以避免编写 Python C 转换代码。
-
我想我用 fd 试过了,但没用,但我会再试一次。感谢您的提示。
-
@CristiFati :再次测试它,是的,实际上 pythons select 适用于 self.fd3。所以不需要 ctypes 选择