Page 1 of 1

socket.gethostbyname returns wrong interface ip add

Posted: Tue Jul 18, 2017 4:17 pm
by Hcgrant
I'm accessing my raspberry pi with "ssh 192.168.254.129 -X -l pi"
I'm running idle3 on the pi ( [email protected]:~ $:idle3 & ) but the idle3 windows are X forwarded to my local comp (suse linux)
In the idle3 shell the command >>>socket.gethostbyname("NeTestPi") yeilds '127.0.1.1' and not the ip add I use to connect to the pi .. 192.168.254.129
If I kill the lo interface it stops the idle3 windows working on my local comp!
I want to use gethostbyname because I'll have a number of pi's gathering temp and humidity data
Help!

Re: socket.gethostbyname returns wrong interface ip add

Posted: Tue Jul 18, 2017 5:51 pm
by davef21370
I'm not clued up enough on networks to give a definitive answer but this may shed some light... http://www.debian.org/doc/manuals/debia ... resolution

Dave.

Re: socket.gethostbyname returns wrong interface ip add

Posted: Tue Jul 18, 2017 6:53 pm
by jojopi
You are running Python on the Pi. On that machine, 127.0.1.1 is a valid address to use to connect to the named machine (which is itself).

Were you planning for the Pi to communicate the lookup result to some other system? How and why? Normally you would do the lookup on the host where you need the result.

(Your X11 connections are not going over the network directly, but are being tunnelled through SSH. Hence they connect first to the local sshd, and depend on the Pi's loopback interface. Downing the interface would not have solved your problem anyway.)

Re: socket.gethostbyname returns wrong interface ip add

Posted: Tue Jul 18, 2017 7:18 pm
by DougieLawson
If you want to find your LAN IP you need to connect the socket to some external server. Easiest is to use your local gateway/router. This example is a bit crude because I'm discovering the GW address by running an ip route command.

Code: Select all

#!/usr/bin/python3
import socket
import os
gw = os.popen("ip -4 route show default").read().split()
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect((gw[2], 0))
ipaddr = s.getsockname()[0]
gateway = gw[2]
host = socket.gethostname()
print ("IP:", ipaddr, " GW:", gateway, " Host:", host)

Re: socket.gethostbyname returns wrong interface ip add

Posted: Tue Jul 18, 2017 9:20 pm
by Hcgrant
Thanks everyone for their answers .
Re DougieLawson .. I ran your program on the pi but it didn't return the IP I was expecting.. The pi has a fixed IP add but the one returned looks like it was obtained by DHCP .

Perhaps a bit more explanation of my aims might help .. there may be a better way of trying to do what I want to do .. (more than likely!)
The program I'm writing is a data sender (in a pi zero w) .. it reads a sensor and then transmits location, date, time , humidity and temperature to a data collector ( with a known fixed IP add ) which sits there receiving data from a group of senders. To create a socket to transmit, I need the IP add of the particular pi zero: I can do this by fixing the IP adds of all the zeros but I would prefer to let DHCP set the IP adds, hence the use of socket.gethostbyname()

Re jojopi .. the pi has two interfaces, 127.0.1.1 and the fixed IP 192.168.254.129. I dont understand why gethostbyname returns 127.0.1.1 (lo) .. is there some way i can directed it to 192.168.254.129 (wlan0)? .. Which will be set by DHCP in the live system.

Re: socket.gethostbyname returns wrong interface ip add

Posted: Tue Jul 18, 2017 9:41 pm
by DougieLawson
How many interfaces are active? Have you been adding static addresses in /etc/network/interfaces rather than in /etc/dhcpcd.conf? Because my program works perfectly on all fourteen of my raspberries.

Re: socket.gethostbyname returns wrong interface ip add

Posted: Tue Jul 18, 2017 9:58 pm
by jojopi
Hcgrant wrote:To create a socket to transmit, I need the IP add of the particular pi zero:
You do not need to know your own IP address to create an outbound connection. You only need to know the address you are connecting to. More specifically, it is completely optional to bind() a local socket before you connect() it.

Even for inbound sockets, which you do need to bind() before listen(), it is not necessary to specify a particular address unless you want to limit connections to that interface. You can just pass the empty string "" to mean any address.

Also, in the Python socket interface you can use hostnames directly in bind() and connect(), rather than look up IP addresses yourself.

Re: socket.gethostbyname returns wrong interface ip add

Posted: Tue Jul 18, 2017 10:09 pm
by Hcgrant
Re DougieLawson .. yes I did configure the static address in /etc/network/interfaces. I'll look at /etc/dhcpcd.conf .. thanks

Re jojopi .. Thanks .. I'm going to have to learn a bit more about making connections.

I still dont know why get gethostbyname returned the lo IP add .. Ive used this construction before .. a year or two ago though , using a pi to monitor my doorbell and tell me its being pressed.

Re: socket.gethostbyname returns wrong interface ip add

Posted: Tue Jul 18, 2017 10:52 pm
by Paeryn
Hcgrant wrote:I still dont know why get gethostbyname returned the lo IP add .. Ive used this construction before .. a year or two ago though , using a pi to monitor my doorbell and tell me its being pressed.
NeTestPi will be assigned to 127.0.1.1 in /etc/hosts
I assume you've got avahi running (it is by default), using the name NeTestPi.local should give you the assigned ip address.

Code: Select all

>>> import socket
>>> print(socket.gethostbyname("rpi3"))
127.0.1.1
>>> print(socket.gethostbyname("rpi3.local"))
192.168.0.40

Re: socket.gethostbyname returns wrong interface ip add

Posted: Wed Jul 19, 2017 8:51 am
by Hcgrant
This is Interesting .. Ive connected a monitor and keyboard directly to the pi and run your program DougieLawson and it returns the static IP address I assigned via interfaces! But gethostbyname still returns 127.0.1.1 ! Grrrrrrr

Re: socket.gethostbyname returns wrong interface ip add

Posted: Wed Jul 19, 2017 11:48 am
by elParaguayo
A lot of this thread is way beyond me but is there anything wrong with using "hostname -I" to get your LAN IP? This is how the Raspberry Pi documentation suggests doing it.

In python you could just do:

Code: Select all

from subprocess import check_output
ip = check_output(["hostname", "-i"])
print("My IP is: {}".format(ip))

Re: socket.gethostbyname returns wrong interface ip add

Posted: Thu Jul 20, 2017 4:32 pm
by Hcgrant
Thanks again for all your inputs .. I think my best soloution is to follow jojopi's advice.
I modified the hosts file -- 192.168.254.129 NeTestPi ( removing the 127.0.1.1 address, which, btw, I was confusing with the lo add ) and gethostbyname not unsuprisingly returned 192.168.254.129. :roll:

Re: socket.gethostbyname returns wrong interface ip add

Posted: Thu Jul 20, 2017 5:51 pm
by DirkS
Hcgrant wrote:removing the 127.0.1.1 address
That may lead to warnings / errors when you use 'sudo'