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

Nobody nobody at nowhere.com
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> (http://linux.die.net/man/2/gethostname) and gethostbyname()
> from #import <netdb.h> (http://linux.die.net/man/3/gethostbyname). 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.:

	192.168.0.2 lieryan.yourdomain.com lieryan

or just:

	192.168.0.2 lieryan

> PS: I found an MSDN article by Microsoft stating that
> gethostbyname(gethostname) is guaranteed to always succeed
> (http://msdn.microsoft.com/en-us/library/ms738527(VS.85).aspx); is this
> guarantee also true in linux?

No.




More information about the Python-list mailing list