Hcgrant
Posts: 17
Joined: Sun Oct 14, 2012 4:51 pm

socket.gethostbyname returns wrong interface ip add

Tue Jul 18, 2017 4:17 pm

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!

User avatar
davef21370
Posts: 897
Joined: Fri Sep 21, 2012 4:13 pm
Location: Earth But Not Grounded

Re: socket.gethostbyname returns wrong interface ip add

Tue Jul 18, 2017 5:51 pm

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.
Apple say... Monkey do !!

User avatar
jojopi
Posts: 3078
Joined: Tue Oct 11, 2011 8:38 pm

Re: socket.gethostbyname returns wrong interface ip add

Tue Jul 18, 2017 6:53 pm

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.)

User avatar
DougieLawson
Posts: 35522
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: socket.gethostbyname returns wrong interface ip add

Tue Jul 18, 2017 7:18 pm

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)
Note: Having anything remotely humorous in your signature is completely banned on this forum. Wear a tinfoil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

Hcgrant
Posts: 17
Joined: Sun Oct 14, 2012 4:51 pm

Re: socket.gethostbyname returns wrong interface ip add

Tue Jul 18, 2017 9:20 pm

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.

User avatar
DougieLawson
Posts: 35522
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: socket.gethostbyname returns wrong interface ip add

Tue Jul 18, 2017 9:41 pm

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.
Note: Having anything remotely humorous in your signature is completely banned on this forum. Wear a tinfoil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

User avatar
jojopi
Posts: 3078
Joined: Tue Oct 11, 2011 8:38 pm

Re: socket.gethostbyname returns wrong interface ip add

Tue Jul 18, 2017 9:58 pm

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.

Hcgrant
Posts: 17
Joined: Sun Oct 14, 2012 4:51 pm

Re: socket.gethostbyname returns wrong interface ip add

Tue Jul 18, 2017 10:09 pm

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.

User avatar
Paeryn
Posts: 2604
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: socket.gethostbyname returns wrong interface ip add

Tue Jul 18, 2017 10:52 pm

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
She who travels light — forgot something.

Hcgrant
Posts: 17
Joined: Sun Oct 14, 2012 4:51 pm

Re: socket.gethostbyname returns wrong interface ip add

Wed Jul 19, 2017 8:51 am

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

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: socket.gethostbyname returns wrong interface ip add

Wed Jul 19, 2017 11:48 am

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))
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

Hcgrant
Posts: 17
Joined: Sun Oct 14, 2012 4:51 pm

Re: socket.gethostbyname returns wrong interface ip add

Thu Jul 20, 2017 4:32 pm

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:

DirkS
Posts: 9871
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: socket.gethostbyname returns wrong interface ip add

Thu Jul 20, 2017 5:51 pm

Hcgrant wrote:removing the 127.0.1.1 address
That may lead to warnings / errors when you use 'sudo'

Return to “Python”