This repository contains a Python OPC-UA client setup using asyncua. The client connects to an OPC-UA server with certificate-based security and reads server namespace and node data.
Reference: asyncua documentation
utils/settings.py- loads environment configuration from.env.utils/opcua_client_cert_gen.py- helper to generate a self-signed client certificate and key.scripts/opcua_test.py- simple OPC-UA polling test without certificate-based security, for server status checking.scripts/opcua_client.py- main async OPC-UA client functions using client certificate/key andutils.settingsconfiguration.scripts/main.py- example entrypoint that can browse server namespace and object nodes.requirements.txt- required Python package dependencies.
- Python 3.11+ recommended
pipinstalled- OPC-UA server endpoint and access credentials/certificates
Clone this project and navigate to the project root directory. Then install the required dependencies:
git clone <repository-url>
cd <repository-directory-on-your-machine>
pip install -r requirements.txtCreate a .env file in the project root with the following values:
- example of
.envcontent:
log_level=INFO
OPC_SERVER_URL=opc.tcp://<server-host>:<port>
PATH_TO_CLIENT_CERT=client_cert.pem
PATH_TO_CLIENT_KEY=client_key.pem
APPLICATION_URI=urn:example.org:FreeOpcUa:opcua-asyncio
TARGET_NAMESPACE=http://your-namespace-uri
TARGET_NODE_ID=ns=3;s=YourNodeUpdate the values to match your OPC-UA server and certificate paths.
Set PYTHONPATH to the project root to ensure utils can be imported.
export PYTHONPATH=$(pwd)From the project root, run:
python scripts/main.pyAfter running for the first time, you will need to accept the client certificate on the server side if it is not already trusted.
(If using Prosys OPC-UA Simulation Server, you can find the pending certificate in the server's Certificates tab, right-click it and choose "Trust".)
Or use module mode to ensure the project root is on Python's import path:
python -m scripts.opcua_clientRun commands from the project root directory. If you execute the script from inside scripts/, Python may fail to import utils.
If you need a self-signed certificate for testing, run:
python ./utils/opcua_client_cert_gen.pyThis generates client_cert.pem and client_key.pem by default.
Reference: text
The file scripts/opcua_test.py shows a simpler connection example using asyncua.Client and polling a node value every second. It is useful for basic connectivity testing.
ModuleNotFoundError: No module named 'utils'- Make sure you run Python from the project root.
- Example:
cd /path/to/project-root python ./scripts/opcua_client.py
- If the OPC-UA server requires a trusted server certificate, you may need to configure
server_certificateinopcua_client.py.
utils/settings.pyusespydantic-settingsto load.envvalues.- The main OPC-UA client uses
asyncua.crypto.security_policies.SecurityPolicyBasic256Sha256andua.MessageSecurityMode.SignAndEncrypt.
Feel free to adapt the .env settings and script logic for your specific OPC-UA server namespace and node IDs.
For questions or issues, please open an issue in this repository or contact the maintainer directly.
- Email: [duongghuy96@gmail.com]