How to bind a raw socket to a specific interface using python in linux centOS?

穿精又带淫゛_ 提交于 2019-12-12 01:55:00

问题


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.



来源:https://stackoverflow.com/questions/27032429/how-to-bind-a-raw-socket-to-a-specific-interface-using-python-in-linux-centos

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!