Skip to main content

USBIP Serial client workflow Python PyPI - Implementation coverage


Overview


This package supports connecting to a CDC (serial) USB device exposed by a USBIPD server. USBIP is a protocol that allows sharing USB devices over a TCP/IP connection. The protocol specification can be found here. This package should be capable of running on any system that supports Python.

There are some issues sharing USB devices to docker containers, a major one being if the USB connection is lost it is difficult to recover the connection between the docker container and the hosting server.

Here's a link that discusses this issue and another solution.

The USBIP client implementation will only address USB devices that implemented the CDC protocol, basically simple serial devices. This allows for a direct connection to the USBIP server without the need of mapping USB devices into the container.

Installation


pip install serial-usbipclient should work for most users.

Usage


from serial_usbipclient.usbip_client import USBIPClient, HardwareID, USBIP_Connection

host: str = 'localhost'
port: int = 3240  # commonly used port for USBIPD servers
target: HardwareID = HardwareID(vid=1234, pid=5678)  # USB devices are identified by VID/PID
client: USBIPClient = USBIPClient(remote=(host, port))
client.connect_server()
client.attach(devices=[target])
connections: list[USBIP_Connection] = client.get_connection(device=target)

# using the established connection, data can be written to the USB device
# using the sendall() method
connections[0].sendall(data=b'\01\02\03\04')

# response data can be read either explicitly by specifying the size of the expected
# response, or if 0, the size is specified, up to a delimiter. The delimiter is a property of
# the connection and can be set, default=b'\r\n'
connections[0].delimiter = b'\n'
response: bytes = connections[0].response_data(size=0)  # reads until delimiter

SOUP


Module Version comments
Python 3.11, 3.12, 3.13, 3.14 Python interpreter(s)
py-datastruct 1.1.0 Serialization of binary to/from dataclasses
type-extensions 4.15.0 extensions for typing (SocketWrapper typing)

Useful Resources


For a Windows version of the usbipd server, look here. You can run this to share USB devices across a network, there are usbipd-clients for Linux & Windows.

Here's a USBIP server for testing written in RUST, https://github.com/jiegec/usbip.

Testing


A MockUSBIP service reads configuration information from the output of lsusb (e.g lsusb -d 1f46:1b01 -v). MockUSBIP will then play back this configuration. Just capture the output of the lsusb command and save with the .lsusb suffix in the test folder. The file name should be the busnum/devnum number.

1-1.lsusb
1-2.lsusb
1-3.lsusb
99-99.lsusb

Would result in 4 devices with busid values of 1-1, 1-2, 1-3 and 99-99. Please note the file 99-99.lsusb is reserved to provide a device to generate failing USBIPD attachments, and 1-2 & 1-3 have the same VID/PID (for testing multiple identical device connections).

During testing, the MockUSBIP service acts as a stand-in for an actual USBIP server. Since the tests are run in parallel, the port on which the service listens must be unique for each unit test. This is accomplished by the conftest.py::pytest_sessionstart which is run when the pytest session is started and collects all tests being run into a file called lists_of_tests.json. The unit test's index into this array is used to determine the offset to be added to the port base (typically 3240). The SocketWrapper class is overridden in unit tests to inject low-level socket failures.

Tooling


This package was created using JetBrains PyCharm Professional IDE, the repository does not contain any IDE specific files. Development work was done using Windows 11 & Python 3.12.5, with final verification on an Ubuntu 20.04 system.

Static code analysis performed using radon and xenon.

Packages required to run tests


Module Version comments
pytest 9.0.0 unit testing framework
pytest-xdist 3.8.0 distributes testing across multiple cpu/cores
coverage 7.11.3 coverage of unit tests
pylint 4.0.2 linter, ensures adherence to PEP-8 standards
pytest-cov 7.0.0 integrates coverage with pytest
pytest-timeout 2.4.0 provides ability to timeout pytest unit tests
mypy 1.18.2 type checking
radon 6.0.1 static code analysis
xenon 0.9.3 static code analysis with thresholds

Packages required to publish to PyPi


Module Version comments
poetry-core 2.2.1 build system

All tooling is defined in the pyproject.toml and managed using poetry as follows:

poetry install --with tests

Tooling is not needed to run the package but is required for testing and packaging.

Build Process


Store the PyPi API token

keyring set https://upload.pypi.org/legacy/ __token__

when prompted, enter the API token you created using PyPi.

To build the distribution and upload to PyPi

poetry build
poetry publish

Metadata

Release files for serial-usbipclient 1.1.2

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

Source distribution (sdist)

Source distribution for serial-usbipclient 1.1.2
File Size Uploaded
serial_usbipclient-1.1.2.tar.gz 28.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for serial-usbipclient 1.1.2
File Interpreter ABI Platform
serial_usbipclient-1.1.2-py3-none-any.whl Python 3 none any Details

Total release size: 57.8 kB

Release files / serial_usbipclient-1.1.2.tar.gz

Download URL serial_usbipclient-1.1.2.tar.gz
Size 28.0 kB
Tags Source
SHA-256 checksum
How to use checksums
4252b14ce2060c9bc6dcb371cd25586016d5749750f7603caa4bdc151b1a3b82
BLAKE2b-256 checksum
How to use checksums
2bde72c5844287b22a12f83e2972882993cdbc3b038e5997b5384a5edd7911b3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.2.1 CPython/3.12.10 Windows/11

Release files / serial_usbipclient-1.1.2-py3-none-any.whl

Download URL serial_usbipclient-1.1.2-py3-none-any.whl
Size 29.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
17eff3fd869c7bf78f9e8e1aac5939ba5160157427ee011554d49d513f93c852
BLAKE2b-256 checksum
How to use checksums
392d6917c802c05707d7b3ec06325f322dde91d537ab374618812dc1a15d42fe
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.2.1 CPython/3.12.10 Windows/11

Release history Release notifications | RSS feed

This release

1.1.2 This release

2 release files

1.1.1

2 release files

1.1.0

2 release files

1.0.7

2 release files

1.0.6

2 release files

1.0.5

2 release files

1.0.4

2 release files

1.0.1

2 release files

1.0.0

2 release files

0.1.6

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

0.0.9

2 release files

0.0.8

2 release files

0.0.7

2 release files

0.0.6

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

0.0.0

2 release files

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