I have a server with two different network interfaces, each with a different IP address. How can I create a socket so it\'ll go out a specific IP address?
I\'d prefe
You can certainly bind a socket to a specific device.
I don't know how to do it in python, but using the berkeley socket api (in C) you need to call setsockopt()
, using the option SO_BINDTODEVICE
.
You pass in an interface descriptor, which is of type struct ifreq
. Ideally you would get the contents of the interface descriptor by using ioctl()
, and requesting SIOCGIFINDEX
- passing the name of the interface (eg. eth0) as an argument.
edit: Just did a quick search and found this documentation of the socket methods in python. setsockopt()
is amongst them.