How to bind a raw socket to a specific interface using python in linux centOS? I have multiple interfaces like eth0, eth0:1, eth0:2,etc
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
You can do it by using the IP address that corresponds to the desired interface.
import socket
s = socket.socket()
s.bind(('192.168.1.100', 12345))
s = socket.socket()
s.bind(('localhost', 12345))
s = socket.socket()
s.bind(('0.0.0.0', 12345))
The first two above would bind to the interface with that IP address. The last one will bind to any interface. You can obtain the IP address for an interface using this recipe.