Darra OPC UA Client — Python SDK
Darra OPC UA Client Python bindings. Advanced cross-language alignment API—the concepts you learn here can be directly applied to C#, Java, C++, Rust, and C.
- Getting Started → Session (Construct / Connect / Read / Write / KeepAlive)
- Browse → Nodes (browse / Path Resolution / register_nodes)
- Real-time → Subscription (create_subscription / MonitoredItem / data_changed)
- History → History (raw / modified / at_time / processed / events / update / delete)
- Call → Method (call)
- Discover → Discovery (get_endpoints / find_servers)
- Data → Data Types (OpcUaVariant / OpcUaDataValue / well_known_nodes)
- Security → Security (Certificates / Encryption)
Installation
- pip (Recommended)
- requirements.txt
- Poetry
pip install darra-opcua
darra-opcua>=0.6.0
poetry add darra-opcua
The Wheel package contains Windows x64 dynamic libraries; after installation via pip, there is no need to manually configure the loading path.
System Requirements
| Item | Requirements |
|---|---|
| Python | 3.8+ (Recommended 3.11) |
| Operating System | Windows 10/11 x64 |
| CPU | x64 |
| Agreement | OPC UA TCP (opc.tcp://) |
Importing Modules
from darra_opcua import ( # Module Name darra_opcua (pip package darra-opcua)
OpcUaSession, # Superclass (= DarraOpcUa Alias)
OpcUaVariant, OpcUaDataValue,
OpcUaSubscription, EventSubscription,
OpcUaNode, NodeCollection, NamespaceCollection, Reference,
OpcUaDiscovery,
MessageSecurityMode, UserTokenType, SessionState,
BuiltinType, NodeClass, AttributeId, MonitoringMode,
StatusCode, OpcUaException,
HistoryAggregate, HistoryUpdateType,
well_known_nodes as wkn,
)
Main Types:
| Type | Uses |
|---|---|
| OpcUaSession | Client base class (= DarraOpcUa; the two are equivalent) |
| OpcUaSubscription | Data Subscription Container (Subscription formerly known as ) |
| EventSubscription | Event Subscriptions (Alarms & Conditions) |
| NodeCollection / OpcUaNode | Lazy Load Node Access |
| OpcUaVariant / OpcUaDataValue | Data Encapsulation |
| OpcUaDiscovery | get_endpoints / find_servers (static method) |
| well_known_nodes (Module) | OPC Foundation Standard node constant; short alias: wkn |
| OpcUaException | Exceptions containing status_code |
Quick Start in One Minute
from darra_opcua import OpcUaSession, OpcUaVariant
with OpcUaSession("opc.tcp://localhost:4840") as ua:
ua.connect()
# Read
with ua.read("ns=2;s=Temperature") as dv:
print(f"Temperature = {dv.value} °C")
# Write (OpcUaVariant hold the resource handle; close it when finished)
with OpcUaVariant().set_double(42.0) as v:
ua.write("ns=2;s=Setpoint", v)
# Subscribe
with ua.create_subscription(500) as sub:
sub.data_changed.append(
lambda e: print(f"{e.node_id} = {e.value_string}"))
sub.add_node("ns=2;s=Counter")
import time
time.sleep(3)
Advanced API Overview
| Feature | Entry API | Description |
|---|---|---|
| Connection / Encryption | OpcUaSession(...) + connect() | NONE / Sign / SignAndEncrypt + Username / Anonymous / Certificate |
| Single-Point Read/Write | ua.read / write | Single Node, Single Attribute |
| Batch Read | ua.read_many(node_ids[, attribute]) | Once RPC, N nodes |
| Browse Tree | ua.browse / browse_many / browse_with_paging / browse_next | Full pagination support |
| Path Resolution | ua.resolve / translate_browse_paths | Browse Path → NodeId |
| Registration Node | ua.register_nodes / unregister_nodes | Reduces server-side parsing overhead during frequent, repeated requests to the same node (benefits based on actual server-side testing) |
| Data Subscriptions | ua.create_subscription + sub.add_node / add_many | publishing / sampling, two-layer |
| Event Subscription | ua.subscribe_events | Alarms & Conditions |
| Adjustment Methods | ua.call(obj_id, mtd_id, [...]) | OPC UA Method Node |
| History Reading | ua.read_history / ua.read_history_* | Raw / Modified / AtTime / Processed / Events |
| Writing History | ua.update_history / ua.delete_history_range | Insert / Replace / Update / Delete |
| Heartbeat | ua.keep_alive_interval_ms | Background Periodic Monitoring |
| Automatic Reconnection | (Built-in) | Check for Disconnections → Republish Subscribe |
| Unified Event | ua.events.* | Session / Subscription / Data / Communication Errors |
| Service Discovery | OpcUaDiscovery.get_endpoints / find_servers | No session required |
| Standard Node | wkn.SERVER_SERVER_STATUS_CURRENT_TIME, etc. | OPC Foundation Standard NodeId Constants |
Key Points About Python
- Context manager (
with):OpcUaSession/OpcUaSubscription/OpcUaDataValue/OpcUaVariantAll supportedwith; resources are automatically released upon exit. Highly Recommended, Do not do this manuallyclose(). - snake_case method names:
ua.read/ua.browse/ua.create_subscription(notRead/Browse). - datetime All UTC: History / Always use
datetime.datetime(tzinfo=timezone.utc) for timestamps; do not use local time. MessageSecurityMode.NONEUppercase: Python keyword conflictNone; useMessageSecurityMode.NONE(remaining valuesSign/SignAndEncryptcase-sensitive like C#).- Callback Thread: The callback is triggered by a background SDK thread.
DataChangeEventArgsA snapshot of the field has already been taken in the callback thread, making it safe for cross-thread use.
Cross-Language Alignment
The API naming and behavior of the C# SDK are the reference implementation for bindings in other languages. The Python binding follows the PEP 8 convention (snake_case + property + context manager), but the semantics are consistent (e.g. C# ua.Read ↔ Python ua.read). Start with Python; switching to another language later only requires renaming — the concepts and semantics stay the same.
Just read through the left sidebar in order to master everything SDK API.