Failed Regression Test: What socket.gethostname() is supposed to return?

Nobody nobody at
Sun Jul 26 04:13:30 EDT 2009

On Sun, 26 Jul 2009 08:30:30 +1000, Lie Ryan wrote:

> since on my system socket.gethostname() returns 'lieryan', and since
> socket.gethostbyname('lieryan') does not resolve to anything; the test
> becomes an error.
> My system is Gentoo, but I think this also happened on Ubuntu (still on
> this laptop). The trunk failed in similar manner.
> Do I have a misconfigured system or is the test faulty?

Misconfigured system.

> I tracked the code for socket.gethostname() and socket.gethostbyname()
> and found that they are simply a wrapper for gethostname() from #import
> <unistd.h> ( and gethostbyname()
> from #import <netdb.h> ( A
> simple test in C found that the C's equivalent to
> gethostbyname(gethostname()) returns a null pointer (used to indicate
> error, per documentation).
> So, the question is: what is socket.gethostname() is supposed to return
> that will be a valid argument for socket.gethostbyname()?

gethostname() returns the hostname.

gethostbyname() returns some information about the host (specifically,
aliases and IP addresses) obtained by platform-specific means (which
normally includes /etc/hosts and DNS, and may also include NIS, LDAP, and
others; see the nsswitch.conf manpage for more details).

Python's socket.gethostname() simply returns the first IP address;
socket.gethostbyname_ex() returns all of the information which libc's
gethostbyname() provides.

In order for gethostbyname() to work, the host must be listed in one of
the databases which it uses. The simplest way to achieve this is to add an
entry to /etc/hosts, e.g.: lieryan

or just: lieryan

> PS: I found an MSDN article by Microsoft stating that
> gethostbyname(gethostname) is guaranteed to always succeed
> (; is this
> guarantee also true in linux?


More information about the Python-list mailing list