pythonopc-uaopc

Connect to OPCUA server with username and password


I am using UAExpert application and i am connecting to my machine with these settings: enter image description here

I want to connect to my device with python. I have this code but it doesn't work.

from opcua import Client

client = Client("opc.tcp://<ip>:4840")
client.set_user("username")
client.set_password("password")
client.set_security_string("Basic256Sha256,Sign,cert.der,key.pem")
client.connect()

I am getting this error:

raise ua.UaError("No matching endpoints: {0}, {1}".format(security_mode, policy_uri)) opcua.ua.uaerrors._base.UaError: No matching endpoints: 2, http://opcfoundation.org/UA/SecurityPolicy#Basic256Sha256

UPDATE:

I think it's the issue of the certificate. So i found from UAExpert settings where it gets the certificate from. enter image description here I use the same path for cert.der but i don't know where i can find the key.pem


Solution

  • Ok so i made it work. This is my current code:

    import asyncio
    import opcua
    
    
    async def main():
        client = opcua.Client("opc.tcp://<ip>:4840", timeout=60)
        client.application_uri = "urn:<ip>:UPS1600"  # Should match in your certificate
        client.set_user("username")
        client.set_password("password")
        client.set_security_string("Basic256Sha256,SignAndEncrypt,<path_to_certificate.pem>,<path_to_key.pem")
        client.connect()
        struct = client.get_node("ns=3;i=100191")
        value= struct.get_value()
        print(value)
    
    
    if __name__ == '__main__':
        asyncio.run(main())
    

    Where certificate.pem and key.pem i have created them from here