Skip to main content

library & utility to intercept i/o from interactive CLI programs

Project description

pyntercept

tl;dr

python library & utility to intercepting and processing i/o of CLI/TUI programs using PTYs.

explanation, possible questions, reasoning

What are pseudo-terminals (PTY)?
It's a UNIX specific (Windows also supports it) form of interprocess communication. Think about it as channel which could provide bidirectional flow of data between two programs. It also provides child process with some terminal services (size, list of supported colors, etc).

Why is that even needed?
It's needed for automatization or situations when you need to run console applications in the environments without a terminal.

Why on earth even run terminal applications without a terminal!?
For example: when you connected to the server via SSH - the server isn't running a terminal, it just sends data through the internet to the client. But some programs on the server expects to be executed in the terminal, to know how exactly to render data (e.g. vim, nano), so SSH server provides pseudo-terminal for them.

It seems like a very niche use case for that, are any other usages for PTYs?
Interesting fact: I didn't use above the term terminal emulator and thats not for brevity. PTYs are used when you don't have a real physical terminal device like VT-100. When you are opening terminal emulator such as Konsole, under the hood there is runned pseudo-terminal.

So the use cases are following:

  • terminal emulators (xterm, konsole, yakuake)
  • ssh servers.
  • terminal multiplexers (GNU screen, tmux, zellij).
  • running terminal applications in environments without controlling terminal e.g. in child processes.
  • i/o processing, e.g. keylogging like GNU script or automation tasks as expect, pyexpect.

The latter two use cases are the main focus for the pyntercept.

how does it work

┌─────────────┐       ┌───────────────┐
│ src (stdin) │       │ dest (stdout) │
└─────────────┘       └───────────────┘
 (1) │                         ^(6)      
     │   ┌─────────────────┐───┘        (0)┌────────────────┐
     └──>│ parent process  │──────────────>│ child process  │
         └─────────────────┘               └────────────────┘
          (2)│    ^(5)                     (4)│        ^(3)
        ┌─── v ── │ ───────────────────────── v ────── │ ────────┐
        │ ┌─────────────┐<─────────────────┌─────────────┐       │
        │ │ parent's fd │                  │ child fd    │       │
        │ └─────────────┘─────────────────>└─────────────┘       │
        │                                        ^               │
        │                  acts like a real terminal for the     │
        │                               child process            │
        │                                                        │
        └──────────────Pseudo terminal (PTY)─────────────────────┘                         
  1. parent process first allocates resources for processes interaction and then runs child process with specified arguments and behaving as proxy object for enteraction with child process.
  2. parent process receives input from source (by default stdin).
  3. then parent could do any transformations to it and even could decide to send it to the child process or not.
  4. child process reads data from step (1) as its stdin.
  5. child process could generate some output and writes it into the child terminal.
  6. parent process reads output sent by child process. Like in step 2, parent process can transform output data and conditionally send it to the destination as step (6).

installation

to install pyntercept as program

pipx install pyntercept

to install pyntercept as library

pip install pyntercept

installing optional dependencies

# if you want rich renderer which also will install pyntercept[pyte]
pip install pyntercept[rich] 
pip install pyntercept[pyte] # if you want pyte (Buffered) renderer

available optional dependencies are:

  • rich
  • pyte

roadmap

  • curses support of 256, true colors.
  • fix rendering with rich python library.
  • support of Don Libes' expect language
  • [DONE] optional dependencies pyntercept[pyte], pyntercept[rich], etc.
  • filters, different rendering stages and strategies to give an ability to pass data into wider spectre of the programs.
  • wider support of external libraries, programs and use cases.
  • [DONE] accumulate differences (like cursor movement or character changes) to optimize rendering and support more environments and use cases.
  • support of Windows and other operating systems.
  • add built-in ability to specify areas of the terminal where to render data.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyntercept-0.0.19.tar.gz (23.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyntercept-0.0.19-py3-none-any.whl (27.9 kB view details)

Uploaded Python 3

File details

Details for the file pyntercept-0.0.19.tar.gz.

File metadata

  • Download URL: pyntercept-0.0.19.tar.gz
  • Upload date:
  • Size: 23.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.3

File hashes

Hashes for pyntercept-0.0.19.tar.gz
Algorithm Hash digest
SHA256 2962a43e3d9e68da66dbec3cb9b25a4e3f6f7bbe76d61ab60ce08d534f14f4f1
MD5 6634fb8cfb9f1d2bb7d1f8ae56cfa830
BLAKE2b-256 ca4a13d17c75c0bf507e97c55adfc66f4ab965b37a5263991104f96aff61dd02

See more details on using hashes here.

File details

Details for the file pyntercept-0.0.19-py3-none-any.whl.

File metadata

File hashes

Hashes for pyntercept-0.0.19-py3-none-any.whl
Algorithm Hash digest
SHA256 231c50604eb023b3aeffa0e830ef8ff6bb820c5b95990745a8b08b83bb6d08c5
MD5 cd5ce079297b1bacaf93d38194702da8
BLAKE2b-256 2e64d1677b04f5ce7e01632201ce97656af5cc1890b008e839229d18bef5de42

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page