Skip to main content

serial_console

PyPI

Serial console is a small utility / library that makes it easier to interface with consoles that use a prompt-like interface.
Instead of simply waiting until the connection times out, serial_console tries to continuously match for a prompt.

# With a simple sh shell present on /dev/ttyS0, 9600 baudrate:
$ serial_console -p '$ ' 'echo hello world!'

hello world!

Prompt matching

serial_console reads byte for byte from the output buffer and tries to match for every new character it receives.
It separates lines based on the OS's default line separator, and only matches on the current line, since prompts normally don't span multiple lines.
Matching can be done on equality, substring, or regular expression matching.
Prompts matched for equality must match the whole line. Substring matching simply looks for the existence of the prompt in the line.
Regular expressions are also available, but may need some extra care to be made efficient.

Regular Expressions

Take the following regular expression, matching most Cisco iOS console prompts:

^([a-z][a-z0-9.-]{0,61}[a-z0-9]|[a-z])(\([a-z0-9 ._-][a-z0-9 ._-]*\))?([>#])$

Executed on every single new character this is a very costly expression.
To try and speed this up, serial_console will look for an "end anchor group" in the regexp.
Most prompts will end in non-alphanumeric characters that may not be commonly found in regular output, like $, #, or ?.
If the regexp contains a group at its end, this group will be matched first before trying to match the whole expression. This will drop most non-matching lines quickly without having to try all iterations from the start of the regexp.
So with the above example expression, serial_console would detect ([>#])$ as the "end anchor" and try to match for [>#]$ first.
The regexp may also contain any amount of spacing characters between the group and the end of the expression, and the group may also be a non-matching group or named group:

(?P<endanchor>[#$>?)]) ?\s*$
# results in the following end anchor regexp:
[#$>?)] ?\s*$

Serial communication

Serial communication is done with the help of the pyserial package and is thus mostly OS independent.
It is also possible to specify console encoding, character mappings (f.i. to map '\r\n' to '\n') and the line seperator (although not with serial_console cli tool).

Note on character mapping

The -m cli argument makes it possible to easily specify one or multiple mappings.
All mappings are applied in the order they have been supplied, but they are applied on the whole last read line on every new character.
So with the following example:

serial_console -m '\r\n' '\n' -m '\r' '\n' ...

Even if the '\r\n' '\n' rule is defined first, the '\r' '\n' rule would match first for a line like foo output\r\n, and result in a mapped string foo output\n\n.

Compatibility with other consoles

While serial_console was mainly intended for use with "dumb" serial connections, it may just as well also be used with any console or interface that has the typical Pythonic IO API.

An "interface" base class is available in serial_console.io.ConsoleIO.
To use a custom ConsoleIO class, simply specify it as the console_io_class named parameter when instantiating a Console object.
Calling Console::open will pass all *args and **kwargs straight through to the constructor of the console_io_class.

import serial_console

class MyCustomConsoleIO(serial_console.io.ConsoleIO):
    def __init__(self, foo_argument, bar_parameter=None):
        ...

console = serial_console.console.Console('(?:[#$]) $', console_io_class=MyCustomConsoleIO)
console.open('foo_argument', bar_parameter=123)

Dependencies

  • Python 3.5: Required for typing type hinting. Python v3+ may also be used together with the typing package.
  • pyserial: Required for serial communication.

Metadata

Release files for hlfbt-serial-console 0.0.6

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for hlfbt-serial-console 0.0.6
File Size Uploaded Provenance Actions
hlfbt-serial-console-0.0.6.tar.gz 12.3 kB Without provenance Details for hlfbt-serial-console-0.0.6.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for hlfbt-serial-console 0.0.6
File Interpreter ABI Platform Actions
hlfbt_serial_console-0.0.6-py3-none-any.whl Python 3 none any Details for hlfbt_serial_console-0.0.6-py3-none-any.whl

Total release size: 24.8 kB

Release files / hlfbt-serial-console-0.0.6.tar.gz

Download URL hlfbt-serial-console-0.0.6.tar.gz
Size 12.3 kB
Tags Source
SHA-256 checksum
How to use checksums
9f04287b0739d94bcfc8c440b5a9babd60429c1c819cd624e18b8fa364fa6b60
BLAKE2b-256 checksum
How to use checksums
3a34c30569eb292248b99cd2872166de40b287e2d8c0dde5acb50445e70664fc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.6

Release files / hlfbt_serial_console-0.0.6-py3-none-any.whl

Download URL hlfbt_serial_console-0.0.6-py3-none-any.whl
Size 12.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
1fa25536352d54320f59ede58ede18dddfed3e45c0cadae379e5e0ec3942afe1
BLAKE2b-256 checksum
How to use checksums
5e5491d031ec5a81e0814e124fb06bdfa5a47dc4069319c0b89895fb3e7ad335
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.6

Security for hlfbt-serial-console 0.0.6

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page