使用用户名和密码连接到OPCUA服务器。

huangapple go评论148阅读模式
英文:

Connect to OPCUA server with username and password

问题

我正在使用UAExpert应用程序,并使用以下设置连接到我的设备:
使用用户名和密码连接到OPCUA服务器。

我想要使用Python连接到我的设备。我有以下代码,但它不起作用。

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()

我遇到了以下错误:

>  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

更新:

我认为这是证书的问题。因此,我从UAExpert设置中找到了它获取证书的位置。
使用用户名和密码连接到OPCUA服务器。
我使用相同的路径来获取 cert.der,但我不知道在哪里找到 key.pem

英文:

I am using UAExpert application and i am connecting to my machine with these settings:
使用用户名和密码连接到OPCUA服务器。

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

from opcua import Client

client = Client(&quot;opc.tcp://&lt;ip&gt;:4840&quot;)
client.set_user(&quot;username&quot;)
client.set_password(&quot;password&quot;)
client.set_security_string(&quot;Basic256Sha256,Sign,cert.der,key.pem&quot;)
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.
使用用户名和密码连接到OPCUA服务器。
I use the same path for cert.der but i don't know where i can find the key.pem

答案1

得分: 2

这是我的当前代码:

import asyncio
import opcua


async def main():
    client = opcua.Client("opc.tcp://<ip>:4840", timeout=60)
    client.application_uri = "urn:<ip>:UPS1600"  # 应该与您的证书匹配
    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())

其中的 "certificate.pem" 和 "key.pem" 是我从这里创建的。

英文:

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

import asyncio
import opcua


async def main():
    client = opcua.Client(&quot;opc.tcp://&lt;ip&gt;:4840&quot;, timeout=60)
    client.application_uri = &quot;urn:&lt;ip&gt;:UPS1600&quot;  # Should match in your certificate
    client.set_user(&quot;username&quot;)
    client.set_password(&quot;password&quot;)
    client.set_security_string(&quot;Basic256Sha256,SignAndEncrypt,&lt;path_to_certificate.pem&gt;,&lt;path_to_key.pem&quot;)
    client.connect()
    struct = client.get_node(&quot;ns=3;i=100191&quot;)
    value= struct.get_value()
    print(value)


if __name__ == &#39;__main__&#39;:
    asyncio.run(main())

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

huangapple
  • 本文由 发表于 2023年1月9日 19:36:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/75056713.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定