Skip to main content

A Python3 MODBUS TCP server for testing purposes

Project description


PyPI PyPI version PyPI Wheel License

A thread-based MODBUS TCP server for testing purposes.


Just type

pip install modbus-tcp-server

And to run it

modbus-tcp-server 502

Port is optional, it will be assumed to be 502 by default.

Just run it without any arguments to see the command line.

If you want verbosity, you can drop -v or -vv after providing port number. Note that in this case giving port number is mandatory.


It does only the commands related to reading and writing analog inputs, discrete inputs, holding registers and coils, so only commands 1-6 and 15-16 are supported.

Also, it spawns a thread per a client. This might be unacceptable to you, however it can also with with gevent.

Client connections will be closed after 60 seconds of inactivity.

Custom data provider

To implement a custom data provider, just extend BaseDataSource to provide data of your choosing and launch it that way:

from import ModbusTCPServer
from modbus_tcp_server.data_source import BaseDataSource

class CustomDB(BaseDataSource):

c_db = CustomDB()

at = ModbusTCPServer('', 502, c_db).start()

Note that since everything is handled in a single thread, if your reads or writes take too long this will hang on them. File a Issue if you've got a problem with that.

Change log


  • made logging received data log level debug
  • socket will be opened with SO_REUSEADDR


  • added automatic connection termination after 60 seconds of inactivity
  • better error detection on connections
  • bugfix: fixed CustomMODBUSError


  • added CustomMODBUSError
  • renamed AcceptThread to ModbusTCPServer


  • added support for commands 15 and 16
  • added exceptions GatewayPathUnavailable and GatewayTargetDeviceFailedToRespond

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

modbus-tcp-server-0.6.tar.gz (9.7 kB view hashes)

Uploaded Source

Built Distribution

modbus_tcp_server-0.6-py2.py3-none-any.whl (11.4 kB view hashes)

Uploaded Python 2 Python 3

Supported by

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