{"id":5119,"library":"asyncua","title":"asyncua: Pure Python OPC-UA Client and Server Library","description":"asyncua is a comprehensive, pure Python library providing both OPC-UA client and server capabilities, designed for asynchronous operations. It enables Python applications to communicate with industrial automation systems using the OPC-UA protocol. Currently at version 1.1.8, the library maintains an active development pace, with frequent bug fixes and feature enhancements, including recent beta releases for upcoming major versions.","status":"active","version":"1.1.8","language":"en","source_language":"en","source_url":"https://github.com/FreeOpcUa/opcua-asyncio","tags":["opc-ua","asyncio","industrial-automation","client","server","protocol"],"install":[{"cmd":"pip install asyncua","lang":"bash","label":"Install stable version"},{"cmd":"pip install --pre asyncua","lang":"bash","label":"Install pre-release version (e.g., 1.2b)"}],"dependencies":[],"imports":[{"symbol":"Client","correct":"from asyncua import Client"},{"symbol":"Server","correct":"from asyncua import Server"},{"note":"Import `ua` directly for convenient access to OPC-UA data types like Variant, NodeId, etc.","wrong":"import asyncua.ua","symbol":"ua","correct":"from asyncua import ua"},{"note":"While Node objects are common, they are usually obtained from Client/Server methods rather than instantiated directly. Direct import is for type hinting or advanced scenarios.","symbol":"Node","correct":"from asyncua.common.node import Node"}],"quickstart":{"code":"import asyncio\nfrom asyncua import Client, ua\nimport os\n\nasync def main():\n    # Replace with your OPC-UA server endpoint\n    server_url = os.environ.get('OPCUA_SERVER_URL', 'opc.tcp://localhost:4840/freeopcua/server/')\n    \n    # Client connection example\n    client = Client(url=server_url)\n    \n    try:\n        await client.connect()\n        print(f\"Connected to OPC-UA server at {server_url}\")\n        \n        # Get the root node\n        root = client.get_root_node()\n        print(f\"Root node: {root}\")\n        \n        # Example: Read a variable (replace with actual node ID)\n        # For example, to read a variable under Objects/MyObject/MyVariable\n        # node_id_str = \"ns=2;i=1001\" # Example NodeId\n        # my_variable = await client.get_node(node_id_str)\n        # value = await my_variable.read_value()\n        # print(f\"Value of {node_id_str}: {value}\")\n        \n        # You can browse nodes, subscribe to data changes, write values, etc.\n        \n    except Exception as e:\n        print(f\"Error connecting or interacting with server: {e}\")\n    finally:\n        print(\"Disconnecting from OPC-UA server...\")\n        await client.disconnect()\n\nif __name__ == '__main__':\n    asyncio.run(main())\n","lang":"python","description":"This quickstart demonstrates how to establish a basic OPC-UA client connection, retrieve the root node, and gracefully disconnect. It uses an environment variable for the server URL, which is a common practice for flexible deployment. Remember that `asyncua` is built on `asyncio`, so all client/server operations must be `await`ed within an `async` function."},"warnings":[{"fix":"Upgrade your Python environment to 3.10 or newer. For projects requiring older Python versions, stick to asyncua version 1.1.x or earlier.","message":"Starting with version 1.2.0 (currently in beta), asyncua removes support for Python versions older than 3.10. Users on Python 3.9 or earlier must upgrade their Python environment before migrating to asyncua 1.2.x or later.","severity":"breaking","affected_versions":">=1.2.0"},{"fix":"Ensure all asyncua methods are called with `await`. For example, use `await client.connect()` instead of `client.connect()`.","message":"asyncua is an asyncio-based library. All network operations (connect, read, write, subscribe, disconnect, etc.) are coroutines and must be properly `await`ed within an `async` function. Forgetting `await` will result in coroutine objects not being run, leading to unexpected behavior or resource leaks.","severity":"gotcha","affected_versions":"All"},{"fix":"Wrap client connection logic in `try...finally` to ensure `await client.disconnect()` is called, or preferably, use `async with Client(...) as client:` which handles connection and disconnection automatically.","message":"Always ensure to call `await client.disconnect()` or use `async with Client(...)` for client connections to gracefully close the connection and release resources. Failing to do so can leave dangling connections and consume system resources unnecessarily.","severity":"gotcha","affected_versions":"All"},{"fix":"When writing values, explicitly construct `ua.Variant` objects with the correct `VariantType` if the default conversion is not sufficient, e.g., `await node.write_value(ua.Variant(123, ua.VariantType.Int32))`.","message":"When interacting with OPC-UA values, ensure correct handling of `asyncua.ua.Variant` and `asyncua.ua.VariantType`. Directly assigning Python types might work for simple cases, but explicit `ua.Variant` usage is often required for specific OPC-UA data types, especially when writing values.","severity":"gotcha","affected_versions":"All"}],"env_vars":null,"last_verified":"2026-04-13T00:00:00.000Z","next_check":"2026-07-12T00:00:00.000Z"}