Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
711 views
in Technique[技术] by (71.8m points)

windows - Can Python select what network adapter when opening a socket?

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

  • ETH 0 Source: 192.168.0.1
  • ETH 0 Destination: 192.168.0.2
  • ETH 1 Source: 10.20.30.1
  • ETH 1 Destination: 10.20.30.2
  • ETH 2 Source: 60.50.40.1
  • ETH 2 Destination: 60.50.40.1

Possible Trouble Case

  • ETH 0 Source: 192.168.0.1
  • ETH 0 Destination: 192.168.0.2
  • ETH 1 Source: 10.20.30.1
  • ETH 1 Destination: 10.20.30.2
  • ETH 2 Source: 192.168.0.3
  • ETH 2 Destination: 192.168.0.2

Additional Information
Adapters ETH0,1,and 2 are all connected to different physical netoworks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...