Pure Python S7 communication library for Siemens PLCs with read optimization pipeline
Project description
About
Python-snap7 is a pure Python S7 communication library for interfacing with Siemens S7 PLCs.
Python-snap7 is tested with Python 3.10+, on Windows, Linux and OS X.
The full documentation is available on Read The Docs.
Version 3.0 - Breaking Changes
Version 3.0 is a major release that rewrites python-snap7 as a pure Python implementation. The C snap7 library is no longer required.
This release may contain breaking changes. If you experience issues, you can pin to the last pre-3.0 release:
$ pip install "python-snap7<3"
The latest stable pre-3.0 release is version 2.1.0.
Fork: Optimized Multi-Read Pipeline
This fork (python-snap7-optimized) adds a transparent nodeS7-style read optimization pipeline to the upstream python-snap7 library.
When read_multi_vars() is called with multiple items targeting standard memory areas (DB, M, I, Q), the library now automatically:
Sorts requests by area, DB number, and byte offset.
Merges adjacent or nearby requests (gap ≤ 5 bytes) into contiguous blocks, eliminating redundant reads of overlapping or neighboring addresses.
Packetizes merged blocks into minimal S7 PDU-sized packets, respecting both request and reply budget limits.
The result is dramatically fewer network round-trips — hundreds of scattered variable reads can collapse into just a few multi-item S7 protocol exchanges.
No API changes required. Existing code using read_multi_vars() benefits automatically. Counter/Timer areas (CT/TM) fall back to individual reads due to their different addressing semantics.
New modules:
snap7/optimizer.py — Pure-logic 3-stage pipeline (sort → merge → packetize).
Extended snap7/s7protocol.py — Multi-item S7 read request building and response parsing.
Extended snap7/server/__init__.py — Server-side multi-item read support for testing.
Parallel Packet Dispatch
For PLCs that support it (S7-1200, S7-400, S7-1500), multiple optimized read packets are dispatched back-to-back on the single TCP connection. Responses are matched by S7 sequence number as they arrive, maximizing throughput.
Auto-tuned max_parallel based on PLC capabilities (CP info or PDU heuristic).
Stale-packet detection with automatic retry.
S7-300 / LOGO / S7-200 automatically fall back to sequential dispatch.
Optimization Plan Caching
The optimization pipeline result (sort → merge → packetize) is cached across repeated read_multi_vars() calls with the same item list. This eliminates re-computation overhead during cyclic polling — only the first call pays the optimization cost; subsequent calls reuse the cached plan.
TCP Keepalive for Fast Dead-Peer Detection
All TCP connections enable SO_KEEPALIVE with aggressive probe settings:
Linux: TCP_KEEPIDLE=2s, TCP_KEEPINTVL=1s, TCP_KEEPCNT=2 — detects dead connections within ~4 seconds.
macOS: TCP_KEEPALIVE=2s.
This catches network-level failures (cable pull, PLC power loss, OS crash) far faster than the default TCP timeout (typically 30–120 seconds), which is critical for industrial applications where data gaps must be minimized.
Heartbeat Read for Redundant PLC Monitoring
New Client.heartbeat_read(timeout_ms=500) method provides a lightweight liveness check designed for monitoring standby PLCs in redundant S7 systems (S7-300H, S7-400H, S7-1500R/H).
client = snap7.Client()
client.connect("192.168.1.11", 0, 1)
# Fast liveness check — reads 1 byte from M0
if client.heartbeat_read(timeout_ms=300):
print("Standby PLC is alive")
else:
print("Standby PLC is unreachable")
Reads 1 byte from Merker area (M0) — negligible PLC scan cycle impact.
Uses a short, independent timeout (default 500 ms) without affecting the client’s normal socket timeout.
Returns True / False — no exceptions on timeout or connection error.
Designed for background heartbeat threads monitoring the inactive CPU in a dual-connection redundancy setup.
Model-Specific Tuning
Read optimization respects PLC-specific limits:
PLC Model |
Max Read Block |
Max Parallel |
|---|---|---|
S7-200 / S7-200 Smart / LOGO |
100 bytes |
1 (sequential) |
S7-300 |
200 bytes |
1 (sequential) |
S7-400 |
200 bytes |
4 |
S7-1200 |
1000 bytes |
8 |
S7-1500 |
1000 bytes |
8 |
Installation
Install using pip:
$ pip install snap7-optimized
No native libraries or platform-specific dependencies are required - this is a pure Python package that works on all platforms (Linux, Windows, macOS).
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file snap7_optimized-3.1.0.tar.gz.
File metadata
- Download URL: snap7_optimized-3.1.0.tar.gz
- Upload date:
- Size: 128.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
858255e1f367f87deeab420f9c031346a011d1fa9b02bac2e492751b01e56dc0
|
|
| MD5 |
bc93c68149bc3b36f97f86a45e0619be
|
|
| BLAKE2b-256 |
eb281c7ec141a15209787b0d11371443efa266226f2916cd738e42cd19516a3d
|
Provenance
The following attestation bundles were made for snap7_optimized-3.1.0.tar.gz:
Publisher:
publish-pypi.yml on QuakeString/python-snap7-optimized
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
snap7_optimized-3.1.0.tar.gz -
Subject digest:
858255e1f367f87deeab420f9c031346a011d1fa9b02bac2e492751b01e56dc0 - Sigstore transparency entry: 1191514386
- Sigstore integration time:
-
Permalink:
QuakeString/python-snap7-optimized@13b6dff9145e8ee34badcc569563311c84e0b742 -
Branch / Tag:
refs/heads/master - Owner: https://github.com/QuakeString
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-pypi.yml@13b6dff9145e8ee34badcc569563311c84e0b742 -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file snap7_optimized-3.1.0-py3-none-any.whl.
File metadata
- Download URL: snap7_optimized-3.1.0-py3-none-any.whl
- Upload date:
- Size: 94.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
882a8054e44454cddd4259aa15dc9c18fc2ef7b4b83c6abde3c950b19aa3c229
|
|
| MD5 |
d6bc4a30a3e3c0d8a4b35c11acc499d5
|
|
| BLAKE2b-256 |
18c189d3426363d10a2b963128e53b61a98848c0a5d1bc6207337eb4a457c7b6
|
Provenance
The following attestation bundles were made for snap7_optimized-3.1.0-py3-none-any.whl:
Publisher:
publish-pypi.yml on QuakeString/python-snap7-optimized
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
snap7_optimized-3.1.0-py3-none-any.whl -
Subject digest:
882a8054e44454cddd4259aa15dc9c18fc2ef7b4b83c6abde3c950b19aa3c229 - Sigstore transparency entry: 1191514478
- Sigstore integration time:
-
Permalink:
QuakeString/python-snap7-optimized@13b6dff9145e8ee34badcc569563311c84e0b742 -
Branch / Tag:
refs/heads/master - Owner: https://github.com/QuakeString
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-pypi.yml@13b6dff9145e8ee34badcc569563311c84e0b742 -
Trigger Event:
workflow_dispatch
-
Statement type: