The target machine running the python application will have three network interfaces available to it. In general all three networks will be vastly different, however there is a possibility that two of the three could be on similar networks.
In the example below I do not have control over the destination address on ETH 2 (as it a pre-configured system), so I forced into selecting what adapter to use programmaticly.
I am fairly sure that this will fall on how the OS works with routing the connections. My hope is that there will be a platform independent way to solve the issue using python, because there is a possibility that this application will need to run on Windows 7 as well as a Linux machine.
Example Code
import socket s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect(('192.168.0.2', 8000)) # Which device will this connect to??
Normal Case
Possible Trouble Case
Additional Information
Adapters ETH0,1,and 2 are all connected to different physical netoworks
I can't speak much for Windows, but on Linux the interface is normally not chosen until a routing decision is made, therefore you usually don't have a say on which interface your packets leave.
You do have the option though, of using SO_BINDTODEVICE
(see man 7 socket
) on Linux. This binds a socket to a device, however, only root can set this option on a socket.
Just checked, and the python socket library doesn't have SO_BINDTODEVICE
defined, but you get it from socket.h
:
# from socket.h # define SO_BINDTODEVICE 25 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.setsockopt(socket.SOL_SOCKET, 25, 'eth0')
See also:
On Windows, if you know the IP address of the interface you want to use, just bind to that before you connect. On Linux,use socket option SO_BINDTODEVICE as suggested by JimB (seems to be a privileged call too).
i.e. on Windows
import socket s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.bind(('192.168.0.1', 0)) s.connect(('...'))
Binding source address under Windows, selects the interface with the same IP address as that device, even if that IP address has a higher routing metric cost. This doesn't work under Linux though, as it always overwrites the source address with the IP address of the selected device. Routing is done based solely on the destination address. The only exception it seems is if you set source address to 127.0.0.1, then Linux prevents these packets from going out of that box.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With