Score:0

Python socket.gethostname() data source

in flag

For a project I'm working on inside of a dockerized python3 module. I'm running into the issue that socket.gethostname() always returns the random docker name. Since these instances are temporary and live only for a short while before re-spawning, the random names build up quite quick and I would like a method for standardizing these names across the board.

My constraints are:

  1. I don't control the upstream source, so modifying it or maintaining my own version is a hard no.
  2. I don't control how docker containers are being configured so setting the name at docker run time is a no go.

The questions I have are:

  1. Where does socket.gethostname() get it's data from. Obviously this is from the system, but where specifically and how? If you change /etc/hosts, it still pulls the previous value.
  2. Is there a way to tell your python env directly that I want to set the hostname value to x regardless of the system set hostname (this may be the ideal if possible)?
  3. What would be the most sane way to handle this?

As well, if someone wanted to point me to the actual source code for socket.gethostname() so I could see how it works for myself it would be appreciated!

Score:1
us flag

I'd expect Python's socket.gethostname() to be just a thin wrapper around the gethostname system call, which on Ubuntu (using glibc) is in turn a wrapper for the uname system call:

C library/kernel differences
   The  GNU  C  library does not employ the gethostname() system call; instead, it implements
   gethostname() as a library function that calls uname(2) and copies up to  len  bytes  from
   the  returned  nodename  field  into  name.

And for uname(2):

   This is a system call, and the operating system presumably knows its  name,  release,  and
   version.   It  also  knows what hardware it runs on.  So, four of the fields of the struct
   are meaningful.  On the other hand, the field nodename is meaningless: it gives  the  name
   of  the present machine in some undefined network, but typically machines are in more than
   one network and have several names.  Moreover, the kernel has no way of knowing about such
   things,  so  it  has  to  be  told what to answer here.  The same holds for the additional
   domainname field.

   To this end, Linux uses the system calls sethostname(2) and setdomainname(2).   Note  that
   there  is no standard that says that the hostname set by sethostname(2) is the same string
   as the nodename field of the struct returned by uname()  (indeed,  some  systems  allow  a
   256-byte  hostname and an 8-byte nodename), but this is true on Linux.  The same holds for
   setdomainname(2) and the domainname field.

So:

  1. It depends on what does sethostname(). In the case of Docker containers, that's Docker itself.

  2. I don't know.

  3. Just ask Docker to set whatever hostname you want for the container:

    % docker run --rm -it python:3.9 python -c 'import socket; print(socket.gethostname())'
    dd247ca179da
    % docker run --rm -it --hostname my-own-hostname python:3.9 python -c 'import socket; print(socket.gethostname())'
    my-own-hostname
    

Here's the relevant portion of the source code, for Python 3.11.3:

#ifdef HAVE_GETHOSTNAME
/* Python interface to gethostname(). */

/*ARGSUSED*/
static PyObject *
socket_gethostname(PyObject *self, PyObject *unused)
{
    if (PySys_Audit("socket.gethostname", NULL) < 0) {
        return NULL;
    }

#ifdef MS_WINDOWS
// snip
#else
    char buf[1024];
    int res;
    Py_BEGIN_ALLOW_THREADS
    res = gethostname(buf, (int) sizeof buf - 1);
    Py_END_ALLOW_THREADS
    if (res < 0)
        return set_error();
    buf[sizeof buf - 1] = '\0';
    return PyUnicode_DecodeFSDefault(buf);
#endif
}
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.