Python OPCUA Server调用start方法报错提示未指定host/port/sock
OPC UA测试服务器启动报错解决方案
错误原因与修复步骤
1. 端点URL语法错误
代码中定义的OPC UA端点URLopc.tcp//192.168.1.5:4841缺少协议分隔符的冒号,不符合opc.tcp协议的URL规范,导致底层asyncio库无法正确解析出主机地址和端口,直接触发ValueError: Neither host/port nor sock were specified报错。
修正方式:将URL定义行修改为:
url = 'opc.tcp://192.168.1.5:4841'
2. 安全策略配置缺失
日志开头的Endpoints other than open requested but private key and certificate are not set.提示是因为服务端默认启用了需要证书验证的安全策略,测试场景下无需加密传输的话,可以显式配置允许无安全策略的连接,避免后续出现权限拦截问题。
修正方式:
- 首先新增ua模块导入:
from opcua import ua
- 在调用
my_server.start()之前添加安全策略配置:
my_server.set_security_policy([ua.SecurityPolicyType.NoSecurity])
修正后完整可运行代码
from opcua import Server, ua import datetime import time my_server = Server() url = 'opc.tcp://192.168.1.5:4841' my_server.set_endpoint(url) name = "OPCUA_TEST_Server" addspace = my_server.register_namespace(name) node = my_server.get_objects_node() param = node.add_object(addspace, "Parameters") t_text1 = param.add_variable(addspace, "Text 1", "Text_1") i_int1 = param.add_variable(addspace, "myInteger1", 0) b_bool1 = param.add_variable(addspace, "myBool1", False) t_text1.set_writable() i_int1.set_writable() b_bool1.set_writable() my_server.set_security_policy([ua.SecurityPolicyType.NoSecurity]) my_server.start() print("Server started at {}".format(url)) print("At" + str(datetime.datetime.now())) while True: time.sleep(0.5)
内容的提问来源于stack exchange,提问作者Carlos Castaño del Castillo
相关产品推荐
相关产品推荐

