Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

pyModbusTCP Tests

A simple Modbus/TCP client and server library for Python.

pyModbusTCP is pure Python code without any extension or external module dependency.

Tests

The module is currently tested on Python 3.10, 3.11, 3.12, 3.13 and 3.14, on Linux, macOS and Windows.

Documentation

Documentation of the latest release is available online at https://pymodbustcp.readthedocs.io/.

Security

Modbus/TCP has no authentication and no encryption: never expose a Modbus/TCP server on the Internet or on an untrusted network. See SECURITY for the protections of the client and the server, the deployment recommendations, the known limitations and how to report a vulnerability.

Setup

Install the package in a virtual environment:

python3 -m venv venv
source venv/bin/activate

From PyPI, the easy way:

# install the latest available release (stable)
pip install pyModbusTCP
# install a specific version (here release 0.3.1)
pip install pyModbusTCP==0.3.1

Pre-release (alpha) of the next version 0.4.0, to test it before the final release: the command line tools, their options and their output may still change. pip never installs a pre-release by default, ask for it explicitly:

# a specific pre-release
pip install pyModbusTCP==0.4.0a3
# or the latest release, pre-releases included
pip install --pre pyModbusTCP

From GitHub:

# install a specific version (here release 0.3.1) directly from github servers
pip install git+https://github.com/sourceperl/pyModbusTCP.git@v0.3.1

Note on the use of versions:

Over time, some things can change. So, it's a good practice to always use a specific version of a package for your project, instead of just relying on the default behavior: without it, the installation tools always install the latest available version of a package. For example, in pyModbusTCP, the automatic TCP open mode (auto_open) is on by default since version 0.2.0, it was off in the previous versions and doesn't exist before version 0.0.12. This can lead to some strange behavior of your application if you are not aware of the change. Look at CHANGES for details on the available versions.

Usage example

See examples/ for full scripts.

Client

from pyModbusTCP.client import ModbusClient

# TCP connection is opened at the first request (auto_open is on by default) and kept open
c = ModbusClient(host='localhost', port=502, unit_id=1)

# or: TCP connection opened before each request and closed after it
c = ModbusClient(host='127.0.0.1', auto_close=True)

Read 2x 16 bits registers at modbus address 0:

regs = c.read_holding_registers(0, 2)

if regs:
    print(regs)
else:
    print('read error: %s' % c.last_error_as_txt)

Write values 44 and 55 to registers at modbus address 10:

if c.write_multiple_registers(10, [44, 55]):
    print('write ok')
else:
    print('write error: %s' % c.last_error_as_txt)

Use the client as a context manager (the TCP connection is closed at the end of the block):

with ModbusClient(host='localhost', port=502) as c:
    print(c.read_coils(0, 8))

Server

from pyModbusTCP.server import ModbusServer

# listen on localhost only (the default host), port 502 needs root privileges on Unix systems: use another one
server = ModbusServer(host='localhost', port=5020, no_block=True)
server.start()

# update the data seen by the clients from the application
server.store.app.holding_registers.write(0, [1, 2, 3])

A server open to the network, with some access restrictions (see SECURITY):

# only the hosts of the 192.168.1.0/24 network can connect, every write request is refused
server = ModbusServer(host='0.0.0.0', port=5020, allowed_hosts=['192.168.1.0/24'], read_only=True)
server.start()

Command line tools

The tools are new in 0.4.0 (only pre-releases for now): without a version or --pre, pip install "pyModbusTCP[cli]" installs the stable 0.3.1, without the tools (pip only shows a warning).

The library comes with command line tools: a Modbus/TCP server, a Modbus/TCP client, and two gateways between Modbus/TCP and Modbus/RTU (serial line). They run from the library itself:

python -m pyModbusTCP.tools server --help
python -m pyModbusTCP.tools client -H 192.168.1.10 read-holding 0 -n 4

# the gateways need pyserial (an optional dependency)
pip install "pyModbusTCP[serial]"
python -m pyModbusTCP.tools tcp2rtu --help
python -m pyModbusTCP.tools rtu2tcp --help

To have them as commands in the PATH (mbtcp-server, mbtcp-client, mbtcp-tcp2rtu and mbtcp-rtu2tcp), install the optional package pyModbusTCP-cli. A plain pip install pyModbusTCP puts no command in the PATH.

# in a virtual environment, with the library
pip install "pyModbusTCP[cli]==0.4.0a3"
# or as isolated command line tools
pipx install pyModbusTCP-cli

Metadata

Release files for pyModbusTCP 0.4.0a3

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

Source distribution (sdist)

Source distribution for pyModbusTCP 0.4.0a3
File Size Uploaded Provenance Actions
pymodbustcp-0.4.0a3.tar.gz 172.5 kB Without provenance Details for pymodbustcp-0.4.0a3.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pyModbusTCP 0.4.0a3
File Interpreter ABI Platform Actions
pymodbustcp-0.4.0a3-py3-none-any.whl Python 3 none any Details for pymodbustcp-0.4.0a3-py3-none-any.whl

Total release size: 293.4 kB

Release files / pymodbustcp-0.4.0a3.tar.gz

Download URL pymodbustcp-0.4.0a3.tar.gz
Size 172.5 kB
Tags Source
SHA-256 checksum
How to use checksums
7431332f95b0b57ab5fbc591f2884a8741b34929b803f699a340c571fc2781e7
BLAKE2b-256 checksum
How to use checksums
0d5a2e2bda0a36db9f35a24c6d818cacc8d162941ee5f1114e0ed02fc77f4e9e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.5

Release files / pymodbustcp-0.4.0a3-py3-none-any.whl

Download URL pymodbustcp-0.4.0a3-py3-none-any.whl
Size 120.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
690db57ae16421633d8b5b288b585f0479317bdaa3ec9debf0372adf402eb50f
BLAKE2b-256 checksum
How to use checksums
d080dfd4ebfb95856be696961b330193b2f28ebfb2631623995689fbcd05db89
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.5

Security for pyModbusTCP 0.4.0a3

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