【发布时间】:2020-08-28 13:57:18
【问题描述】:
我正在尝试跨一些代码移动以使用 asyncio(出于各种原因)。在我现有的代码中,我创建了一个套接字,我将其bind() 连接到特定接口,以确保在该接口上建立 TCP 连接:
_TCP_Socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
_TCP_Socket.bind(('169.254.6.1', 13000)) # All data sent from this IP, Port (i.e using the interface that has this IP)
_TCP_Socket.connect(('169.254.10.123, 13000))
上述方法有效,但我如何在 asyncio 中实现相同的效果?即如何选择要发送的接口? asyncio.open_connection() 没有 bind() 这样的。
那么,我要给它一个套接字吗?即像以前一样创建一个绑定到我要发送的接口的套接字,然后调用open_connection?
reader, writer = await asyncio.open_connection('169.254.10.123', 13000, sock=_TCP_Socket) # Use _TCP_Socket created earlier?
【问题讨论】:
标签: python sockets python-asyncio