如何使用ICE协议

0 投票
2 回答
1766 浏览
提问于 2025-04-26 18:46

我正在尝试在两个被NAT(网络地址转换)保护的电脑之间建立连接。还有一台第三台电脑可以被这两台电脑访问。

我想使用ICE(交互式连接建立)协议,但在Python中找不到任何示例。我听说过pjsip,它包含一个叫pjnath的C语言库,但那也是用C写的。

有没有什么工具可以在Python中实现这个?如果没有,还有没有其他方法可以实现我描述的功能?如果没有,如何在Python中启动ICE协议呢?

暂无标签

2 个回答

0

你可以使用下面这个只用Python写的库来建立你的ICE连接:

https://github.com/jlaine/aioice

这里有一个例子,展示了在同一个程序中如何使用两个ICE端点。在实际应用中,你需要一些信号传递的方法来交换候选者信息、用户名("ufrag")和密码("pwd")这三者。

import asyncio

import aioice


async def connect_using_ice():
    conn_a = aioice.Connection(ice_controlling=True)
    conn_b = aioice.Connection(ice_controlling=False)

    # invite
    await conn_a.gather_candidates()
    conn_b.remote_candidates = conn_a.local_candidates
    conn_b.remote_username = conn_a.local_username
    conn_b.remote_password = conn_a.local_password

    # accept
    await conn_b.gather_candidates()
    conn_a.remote_candidates = conn_b.local_candidates
    conn_a.remote_username = conn_b.local_username
    conn_a.remote_password = conn_b.local_password

    # connect
    await asyncio.gather(conn_a.connect(), conn_b.connect())

    # send data a -> b
    await conn_a.send(b'howdee')
    data = await conn_b.recv()
    print('B got', data)

    # send data b -> a
    await conn_b.send(b'gotcha')
    data = await conn_a.recv()
    print('A got', data)

    # close
    await asyncio.gather(conn_a.close(), conn_b.close())


asyncio.get_event_loop().run_until_complete(connect_using_ice())
0

PjSIP有一个可以用的Python模块。

你可以在这里找到详细信息和相关教程的链接。

撰写回答