Thursday, July 01, 2010

[Py] pty module testing note

The pty module can be used for pseudo terminals, about which I actually know very little.

Here are some simple tests I have conducted in my own Ubuntu PC, and I write down it just as a reminding note.

---
Open pseudo terminal pairs

At the beginning, check the content of /dev/pts:
$ ls /dev/pts
0 1 ptmx

Open another terminal for IPython and then test with pty module as the following:
In [1]: import pty

In [2]: pty.openpty()
Out[2]: (3, 4)

Now check /dev/pts again:
$ ls /dev/pts
0 1 2 ptmx

Go back to IPython:
In [3]: pty.openpty()
Out[3]: (5, 6)

The content of /dev/pts becomes:
$ ls /dev/pts
0 1 2 3 ptmx

Exit IPython, then check the /dev/pts:
$ ls /dev/pts
0 1 ptmx

---
Read and Write test

To read from the pseudo terminal, it seems necessary to write something first.
In [1]: import pty

In [2]: pty.openpty()
Out[2]: (3, 4)

In [3]: pty._writen(3,"test")

In [4]: pty._read(3)
Out[4]: 'test'

---
Ref:
how to use /dev/ptmx for create a virtual serial port?
Python: module pty