You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

OPC UA Server自定义端点设置报错:绑定地址无效问题求解

OPC UA Server自定义端点绑定失败问题解决

问题背景

单板计算机(SBC)与边缘设备通过LAN连接至网关,边缘设备上已运行OPC UA Server。SBC负责采集并分析数据,需通过自身的OPC UA Server将数据提供给边缘设备(SBC分析时需从边缘设备获取部分信息,此为另一议题)。

已创建包含所需节点的服务器,但设置自定义端点opc.tcp://10.208.0.235:4850时启动失败,使用opc.tcp://localhost、opc.tcp://127.0.0.1、opc.tcp://0.0.0.0作为端点时服务器可正常运行。

错误信息

Executing <Task pending name='Task-1' coro=<main() running at C:\...\opcua-test.py:18> wait_for=<GatheringFuture pending cb=[Task.task_wakeup()] created at C:\Users\...\AppData\Local\Programs\Python\Python312\Lib\asyncio\tasks.py:702> cb=[_run_until_complete_cb() at C:\Users\...\AppData\Local\Programs\Python\Python312\Lib\asyncio\base_events.py:180] created at C:\Users\...\AppData\Local\Programs\Python\Python312\Lib\asyncio\runners.py:100> took 1.125 seconds
OPC UA Server(opc.tcp://10.208.0.235) error starting server
Traceback (most recent call last):
  File "C:\Users\...\AppData\Local\Programs\Python\Python312\Lib\site-packages\asyncua\server\server.py", line 550, in start
    await self.bserver.start()
  File "C:\Users\...\AppData\Local\Programs\Python\Python312\Lib\site-packages\asyncua\server\binary_server_asyncio.py", line 139, in start
    self._server = await asyncio.get_running_loop().create_server(self._make_protocol, self.hostname, self.port)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\...\AppData\Local\Programs\Python\Python312\Lib\asyncio\base_events.py", line 1539, in create_server
    raise OSError(err.errno, 'error while attempting '
OSError: [Errno 10049] error while attempting to bind on address ('10.208.0.235', 0): die angeforderte adresse ist in diesem kontext ungültig
Traceback (most recent call last):
  File "C:\Users\...\opcua-test.py", line 25, in <module>
    asyncio.run(main(), debug=True)
  File "C:\Users\...\AppData\Local\Programs\Python\Python312\Lib\asyncio\runners.py", line 194, in run
    return runner.run(main)
           ^^^^^^^^^^^^^^^^
  File "C:\Users\...\AppData\Local\Programs\Python\Python312\Lib\asyncio\runners.py", line 118, in run
    return self._loop.run_until_complete(task)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\...\AppData\Local\Programs\Python\Python312\Lib\asyncio\base_events.py", line 664, in run_until_complete
    return future.result()
           ^^^^^^^^^^^^^^^
  File "C:\...\opcua-test.py", line 18, in main
    async with server:
  File "C:\Users\...\AppData\Local\Programs\Python\Python312\Lib\site-packages\asyncua\server\server.py", line 219, in __aenter__
    await self.start()
  File "C:\Users\...\AppData\Local\Programs\Python\Python312\Lib\site-packages\asyncua\server\server.py", line 554, in start
    raise exp
  File "C:\Users\...\AppData\Local\Programs\Python\Python312\Lib\site-packages\asyncua\server\server.py", line 550, in start
    await self.bserver.start()
  File "C:\Users\...\AppData\Local\Programs\Python\Python312\Lib\site-packages\asyncua\server\binary_server_asyncio.py", line 139, in start
    self._server = await asyncio.get_running_loop().create_server(self._make_protocol, self.hostname, self.port)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\...\AppData\Local\Programs\Python\Python312\Lib\asyncio\base_events.py", line 1539, in create_server
    raise OSError(err.errno, 'error while attempting '
OSError: [Errno 10049] error while attempting to bind on address ('10.208.0.235', 0): the requested address is invalid in this context

服务器启动代码

import asyncio
import logging
from asyncua import Server


async def main():
    _logger = logging.getLogger("asyncua")
    # setup server
    server = Server()
    server.set_server_name("Custom Endpoint Server")
    server.set_endpoint("opc.tcp://10.208.0.235:4850")
    # works with "opc.tcp://localhost", "opc.tcp://127.0.0.1", "opc.tcp://0.0.0.0"

    await server.init()

    async with server:
        while True:
            await asyncio.sleep(1)


if __name__ == '__main__':
    logging.basicConfig(level=logging.DEBUG)
    asyncio.run(main(), debug=True)

解决方案

  • 确认IP归属:检查SBC的网卡是否绑定了10.208.0.235。Windows执行ipconfig,Linux执行ip addr或ifconfig,查看当前网络接口的IP列表,确保该IP存在。
  • 检查端口占用:确认4850端口是否被其他程序占用。Windows用netstat -ano | findstr :4850,Linux用ss -tulpn | grep :4850,若被占用,更换端口或关闭占用程序。
  • 拆分绑定与端点暴露:错误日志显示端口被解析为0,可能是asyncua的端点解析问题。可以先绑定到所有网卡(0.0.0.0),再修改对外暴露的端点地址:
    import asyncio
    import logging
    from asyncua import Server
    
    
    async def main():
        _logger = logging.getLogger("asyncua")
        # setup server
        server = Server()
        server.set_server_name("Custom Endpoint Server")
        # 绑定到所有网卡
        server.set_endpoint("opc.tcp://0.0.0.0:4850")
    
        await server.init()
    
        # 修改端点的访问地址为实际IP
        for endpoint in server.endpoints:
            if endpoint.EndpointUrl.startswith("opc.tcp://0.0.0.0:4850"):
                endpoint.EndpointUrl = endpoint.EndpointUrl.replace("0.0.0.0", "10.208.0.235")
    
        async with server:
            while True:
                await asyncio.sleep(1)
    
    
    if __name__ == '__main__':
        logging.basicConfig(level=logging.DEBUG)
        asyncio.run(main(), debug=True)
    
  • 开放防火墙端口:检查SBC的防火墙规则,允许4850端口的TCP入站连接。Windows在防火墙高级设置中添加入站规则,Linux执行ufw allow 4850/tcp(根据防火墙工具调整)。

内容的提问来源于stack exchange,提问作者Stefan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.02 05:14:55