How to make pty work with control codes python?

Viewed 69

I have a frontend application running xtermjs connected to a pty through websocket in a python backend. The problem is that control codes sent from xtermjs ( such as ctrl-c) are not working.

A minimal example of the backend looks like this:

import pty
import subprocess
import os

master,slave = pty.openpty()

proc = subprocess.Popen(
    ['sleep','5'],
    stdin=slave,
    stdout=slave,
    stderr=slave
)

os.write(master,'\x03'.encode()) # sending ctrl-c, this is how xtermjs sends it
print(os.read(master,1024))
print(proc.wait()) # it should return immediately since sleep has been cancelled.  

Output:

b'^C'
0

It is visible from the output that the terminal recognizes the control code and yet the sleep command doesn't get cancelled. I would expect it to get cancelled.

What am I missing here?

0 Answers
Related