python-snap7 is an open-source Python library that reads and writes the memory of Siemens S7 PLCs over the classic S7 protocol (ISO-on-TCP, port 102). Since version 3.0, published on PyPI in March 2026, it is written entirely in Python: the native Snap7 C library that earlier versions wrapped is no longer used, so the same pip install works on a Windows laptop, a Raspberry Pi or a slim Linux container. On an S7-1200 or S7-1500 it can exchange data blocks (DBs) with your script once two settings are changed in TIA Portal. This guide uses version 3.5.0, the current release in November 2026, and a fictional pasteurising line at an invented dairy, Caseificio Valfredda.
In particolar modo vedremo:
What changed in python-snap7 3.x
Scripts written for 2.x mostly run unchanged, since the names stay the same: Client, db_read, db_write and the util getters and setters. What changed is underneath and around it:
- No DLL or
.soto install. The TPKT, COTP and S7 layers are implemented in Python. Python 3.10 or newer is required; the licence is MIT. - The recommended import is now
from s7 import Client. Old code withimport snap7keeps working, because both names point to the same classes. - Version 3.1 added an
AsyncClientfor asyncio, typed helpers such asdb_read_real()anddb_write_int(), address strings like"DB10.DBD4:REAL", and automatic reconnection with a heartbeat. - A built-in S7 server emulator lets you test without hardware.
Check the installed version with python -c "import s7; print(s7.__version__)".
python-snap7, s7commplus or OPC UA?
TIA Portal talks to a 1200 or 1500 through S7CommPlus. The older S7 protocol that python-snap7 implements is still answered by these CPUs, but only when the PUT/GET option is switched on, and only for part of the memory. That limit decides which tool fits:
| python-snap7 3.x | s7commplus | OPC UA (e.g. asyncua) | |
|---|---|---|---|
| Protocol | Classic S7 via PUT/GET | S7CommPlus (native) | OPC UA server built into the CPU |
| Optimized DBs | No | Yes | Yes |
| Addressing | DB number + byte offset | Symbolic | Symbolic (nodes) |
| PLC-side setup | PUT/GET on, DB not optimized | Check the project’s validated firmware list | OPC UA server enabled and configured |
| Maturity | Project in use for years; the pure-Python 3.x code dates from 2026 and has passed the project’s acceptance tests on a CPU 1212C (FW V4.7.3) and a CPU 1511F-1 PN (FW V2.9.7) | Young project (0.6.0 on PyPI) | Industry standard; on S7-1500 from firmware 2.0 |
| Install | pip install python-snap7 |
pip install s7commplus |
pip install asyncua |
For a data logger, a test bench or a maintenance tool on a machine you control, python-snap7 is the quickest route. If the project forbids PUT/GET or uses optimized DBs everywhere, pick one of the other two.
Step-by-step: read and write a DB from Python
The example DB is DB10 “PASTEUR_LINE” on an S7-1200. The last column matters later, when we write.
| Name | Type | Offset | Written by |
|---|---|---|---|
| PumpRunning | Bool | 0.0 | PLC |
| AlarmActive | Bool | 0.1 | PLC |
| RecipeNo | Int | 2.0 | Python |
| TempSetpoint | Real | 4.0 | Python |
| TempActual | Real | 8.0 | PLC |
| PieceCount | DInt | 12.0 | PLC |
| BatchCode | String[20] | 16.0 | Python |
A String[20] takes 22 bytes (two header bytes for maximum and current length, then the characters), so the DB is 38 bytes long.
- Switch on PUT/GET. Open the CPU properties in TIA Portal, go to Protection & Security > Connection mechanisms and tick Permit access with PUT/GET communication from remote partner.
- Disable optimized access on the DB. Right-click DB10, Properties > Attributes, untick Optimized block access. Do the same on every DB Python touches, including the small command DB11 used below. This reinitialises the DB: every value goes back to its start value, so do it before commissioning or save the actual values first.
- Compile and download, then reopen the DB. Non-optimized DBs show an Offset column: those numbers are your Python addresses. Write them down again after every change to the DB.
- Install the library in a virtual environment:
python -m venv .venv .venvScriptsactivate # Windows source .venv/bin/activate # Linux / macOS pip install "python-snap7>=3.5,<4" - Connect and read the whole DB in one call, then decode it locally. S7-1200 and S7-1500 use rack 0, slot 1.
from s7 import Client from s7.util import get_bool, get_int, get_real, get_dint, get_string PLC_IP = "192.168.0.20" DB_NUM, DB_SIZE = 10, 38 with Client() as plc: # disconnects on exit, even after an error plc.connect(PLC_IP, 0, 1) # rack 0, slot 1, TCP port 102 raw = plc.db_read(DB_NUM, 0, DB_SIZE) # bytearray of 38 bytes pump_on = get_bool(raw, 0, 0) # DBX0.0 alarm = get_bool(raw, 0, 1) # DBX0.1 recipe = get_int(raw, 2) # DBW2 setpoint = get_real(raw, 4) # DBD4 temp = get_real(raw, 8) # DBD8 pieces = get_dint(raw, 12) # DBD12 batch = get_string(raw, 16) # String[20] print(f"{batch}: {temp:.1f} °C, {pieces} pcs, alarm={alarm}")The getters handle the big-endian byte order of the S7. For a few scattered values,
plc.read_tags(["DB10.DBX0.1:BOOL", "DB10.DBD8:REAL"])groups them into as few requests as possible. - Write only what Python owns. A write touches exactly the bytes you send, so writing the 4 bytes of
TempSetpointleaves the rest of the DB alone:from s7.util import set_real with Client() as plc: plc.connect(PLC_IP, 0, 1) buf = bytearray(4) set_real(buf, 0, 72.5) plc.db_write(DB_NUM, 4, buf) # DBD4 only plc.db_write_int(DB_NUM, 2, 5) # typed helper plc.write_tag("DB10:16:STRING[20]", "VF-2611-B") # address string plc.db_write_bool(11, 0, 0, True) # DB11.DBX0.0, a byte the PLC never writes - Test against the emulator before touching the plant. The bundled server answers like a PLC. Ports below 1024 need admin rights on Linux, hence 1102:
from s7 import Server from s7.type import SrvArea srv = Server() srv.register_area(SrvArea.DB, 10, bytearray(38)) srv.register_area(SrvArea.DB, 11, bytearray(2)) srv.start(tcp_port=1102) # in the client: plc.connect("127.0.0.1", 0, 1, tcp_port=1102)
Pitfalls that cost commissioning time
- Bits are written as whole bytes.
db_write_bool()andwrite_tag()on a Bool read the byte, change one bit and write the byte back. If the PLC changes another bit of that byte in between, your write erases it. Keep Python command bits in bytes (or a separate DB, as DB11 above) that the PLC program never writes. - Large reads are split. A read bigger than the negotiated PDU (see
plc.get_pdu_length()) is sent as several requests, so fields at the start and end of a big DB can come from different PLC scans. - One client per thread.
Clientis not thread-safe: give each thread its own connection, or protect a shared one with a lock. - Errors. Catch
s7.error.S7Error, the base class. In the emulator, reading past the end of DB10 raisesS7ProtocolErrorwith “Invalid address (0x05)”, and a DB that does not exist gives “Object does not exist (0x0a)”; codes from a real CPU may differ. If the connection opens but reads are refused on every DB, check PUT/GET first; if only one DB fails, check its Optimized block access attribute. - Security. PUT/GET has no authentication: anyone who reaches port 102 can read and write the same DBs as your script. Keep the PLC on a separate network segment, allow port 102 only from the host running Python, and let the PLC program check the range of every value it receives.
Official sources: the package on PyPI, the python-snap7 documentation and the s7commplus documentation.
Frequently asked questions (FAQ)
Can python-snap7 read optimized DBs?
No. The classic S7 protocol addresses data by DB number and byte offset, and optimized DBs have no fixed offsets. Either untick Optimized block access or switch to s7commplus or OPC UA.
Do I still need snap7.dll or libsnap7.so?
Not from version 3.0 on. If a legacy script misbehaves after the upgrade, pip install "python-snap7<3" brings back the last C-based release while you investigate.
Which rack and slot do I use?
Rack 0, slot 1 for S7-1200 and S7-1500. For comparison, an S7-300 is usually 0/2 and an S7-400 often 0/3, depending on the rack layout.
Why does 71.6 come back as 71.5999984741211?
A Siemens Real is a 32-bit float, while a Python float has 64 bits, so the conversion exposes the rounding of the PLC value. Round for display, compare with a tolerance, or declare the variable as LReal and read it with get_lreal() when you need more precision.

Alessandro Strongoli has been programming PLCs and commissioning machines and plants for many years: S7-1200/S7-1500 with TIA Portal, WinCC panels, SINAMICS drives. Writing here since 2021. About the author
Parti gratis, poi i corsi passo-passo per installarlo e programmarlo.
Corso GRATIS su UdemyCorso completo: Come installare e aggiornare il TIA Portal
Libri: PLC S7-1200 Guida Completa · PLC per Principianti
Percorso: Infarinatura PLC Siemens


