跳到主要内容

get_endpoints

前置 / 配套

签名​

@staticmethod
def get_endpoints(endpoint_url: str,
timeout_ms: int = 10_000) -> List[EndpointDescription]: ...

通过临时 SecureChannel (None 模式) 连服务端, 走完 TCP → Hello/Ack → OPN(None) → GetEndpoints → CLO 就断开. 不需要 Session.

对应规范段: OPC UA Part 4 §5.4.4 (GetEndpoints)。

参数​

参数默认说明
endpoint_url—目标 endpoint, 如 "opc.tcp://server:4840"; 空字符串抛 ValueError
timeout_ms10_000超时 (毫秒); 传 0 时按 10 000 ms 处理

返回 List[EndpointDescription], 服务端一个都没公布时是空列表 (不抛异常)。

EndpointDescription 字段​

字段类型说明
endpoint_urlOptional[str]实际 endpoint URL
security_modeMessageSecurityModeNONE / Sign / SignAndEncrypt
security_policy_uriOptional[str]算法套件 URI
server_application_uriOptional[str]Server 应用 URI
server_product_uriOptional[str]Server 产品 URI
server_application_nameOptional[str]Server 应用名
server_application_typeint0=Server / 1=Client / 2=ClientAndServer / 3=DiscoveryServer
server_certificateOptional[bytes]服务端证书 (DER)
transport_profile_uriOptional[str]传输 Profile (通常 UA-TCP)
security_levelint服务端推荐优先级
user_tokensList[UserTokenPolicy]支持的登录方式

UserTokenPolicy 字段​

字段类型说明
policy_idOptional[str]服务端给的 ID, 客户端激活时传回
token_typeUserTokenTypeAnonymous / Username / Certificate / Issued

用法​

from darra_opcua import OpcUaDiscovery

endpoints = OpcUaDiscovery.get_endpoints("opc.tcp://server:4840")

print(f"Server has {len(endpoints)} endpoints:")
for ep in endpoints:
print(f"\n URL: {ep.endpoint_url}")
print(f" Mode: {ep.security_mode.name}")
print(f" Policy: {ep.security_policy_uri}")
print(f" Tokens:")
for t in ep.user_tokens:
print(f" - {t.token_type.name} (policy_id={t.policy_id})")

典型输出​

Server has 4 endpoints:

URL: opc.tcp://server:4840
Mode: NONE
Policy: http://opcfoundation.org/UA/SecurityPolicy#None
Tokens:
- Anonymous

URL: opc.tcp://server:4840
Mode: Sign
Policy: http://opcfoundation.org/UA/SecurityPolicy#Basic256Sha256
Tokens:
- Anonymous
- Username

URL: opc.tcp://server:4840
Mode: SignAndEncrypt
Policy: http://opcfoundation.org/UA/SecurityPolicy#Basic256Sha256
Tokens:
- Username
- Certificate

服务端通常暴露多个组合 (NONE / Sign / SignAndEncrypt × Anonymous / Username / Certificate), 客户端按需求挑.

异常​

失败一律抛 OpcUaException, 用 e.status_code 判类型 (值是 StatusCode 枚举; 枚举没收录的码保留原始 int)。

异常status_code触发
ValueError("endpoint_url is required")—endpoint_url 为空
OpcUaExceptionBadCommunicationError连不上服务端 / 传输中断
OpcUaExceptionBadTimeouttimeout_ms 内没收到响应
OpcUaExceptionBadEncodingError / BadDecodingError请求编码或响应解析失败
OpcUaException服务端返回的 serviceResult例如 BadServiceUnsupported (服务端不支持 Discovery), 原值透传

实现限制​

  • 同步阻塞调用, 超时由 timeout_ms 控制; 没有取消参数
  • 一次最多返回 256 条 EndpointDescription, 服务端公布得更多时只取前 256 条且不报错
  • 不建 Session: 调完即断, 不占服务端会话配额, 也不需要先 connect()

跨语言对照​

C#PythonJavaC++RustC
GetEndpointsget_endpointsgetEndpointsDiscovery::GetEndpointsDiscovery::get_endpointsDarraUa_Discovery_GetEndpoints

下一步​