英文:
Connect to OPCUA server with username and password
问题
我正在使用UAExpert应用程序,并使用以下设置连接到我的设备:
我想要使用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设置中找到了它获取证书的位置。
我使用相同的路径来获取 cert.der
,但我不知道在哪里找到 key.pem
。
英文:
I am using UAExpert application and i am connecting to my machine with these settings:
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.
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("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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论