Python sending package through wrong interface

Viewed 106

I have a raspberry pi that is both connected to the internet via Wlan and a local device via Ethernet. So it has two IPs; one for each endpoint.

This is how it looks like simplified when running ifconfig; with different IPs for privacy

eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500 
        inet 189.168.200.110  netmask 0.0.0.0  broadcast 255.255.255.255 
    
 
wlan0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500 
        inet 189.168.201.79  netmask 255.255.255.0  broadcast 192.168.1.255 
        

This is the code that python is using to send a message to the device through the Ethernet with that gateway's ip

    TCP_PORT = 3001
    SERVER_IP_AD = "189.168.200.110"
    CLIENT_IP_AD = "189.168.200.155"
    BROADCAST_IP = "255.255.255.255"

    def sendMessage(self, file_path, client_ip=CLIENT_IP_AD):
        print('message en route')
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.bind((self.SERVER_IP_AD, 0))
        s.connect((client_ip, self.TCP_PORT)) #**ERROR IS HERE**

        MESSAGE = self.openFile(file_path)
        s.send(MESSAGE.encode())
        data = s.recv(self.BUFFER_SIZE)
        s.close()
        return data

Using wireshark I can see that the package is being sent through the Wlan interface instead of the Ethernet interface with the correct IP source and IP destination.

How do I tell python to use the correct interface when sending out the package?

1 Answers

In my opinion, you can establish Tcp connection with Ethernet, cause there isn't shaking hands by Ethernet And, you shouldn't use s.bind() and s.connect() at the same time. Because the former is for UDP client, and the later is for TCP client. Have a try with only s.bind().

Related