I'm trying to create a simulated TTY connected target device that I can connect to via i.e. minicom. I'm using /dev/ptmx
to create a pty and print out the slave name to be opened by i.e. minicom:
Please connect to: /dev/pts/4
. On the python side I then use os.read and os.write to do io and simulate my target:
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)
The whole process is quite combersome. I would like to use select
on the python side which forces my to use ctypes again. I cannot wrap the self.fd3 into a file using os.fdopen
because I need to prevent close
being called on self.fd3.
So I have two questions:
- Does anyone know a readymade python library that handles pty creation and manipulation when implementing the pty master side in python?
- If not: Is there a example somewhere that describes howto call libc.select via ctypes?