[英]How to use the ICE protocol
I'm trying to make a connection between two computers behind NAT. 我正在尝试在NAT后面的两台计算机之间建立连接。 I have a third computer that is accessible by those two.
我有一台第三台计算机,这两台计算机都可以访问。
I would like to use the ICE (Interactive Connectivity Establishment) protocol, but I can't find any example in Python. 我想使用ICE(交互式连接建立)协议,但在Python中找不到任何示例。 I heard about
pjsip
, which include a C library called pjnath
, but that is also written in C. 我听说过
pjsip
,其中包括一个名为pjnath
的C库,但这也是用C编写的。
Are there any tools to make it in Python? 有什么工具可以用Python制作吗? If not, is there another way to do what I described?
如果没有,还有另一种方法可以执行我描述的操作吗? If not, how to start ICE protocol in Python?
如果没有,如何在Python中启动ICE协议?
You can use the following Python-only library to establish your ICE connection: 您可以使用以下仅Python的库来建立ICE连接:
https://github.com/jlaine/aioice https://github.com/jlaine/aioice
Here is an example of two ICE endpoints in the same process. 这是同一过程中两个ICE端点的示例。 In real life you need some signaling method to exchange the candidates, username ("ufrag") and password ("pwd") between the two.
在现实生活中,您需要某种信令方法来在两者之间交换候选者,用户名(“ 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())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.