I think it just returns whatever the underlying [`gethostname()` call][1] does. For example (though this is on Linux):
    # hostname foobar
    # python -c 'import socket; print(socket.gethostname())'
    foobar
    # hostname foobar.example.org
    # python -c 'import socket; print(socket.gethostname())'
    foobar.example.org
    
If you just want the hostname part, it's not hard to remove the rest:
    # python -c 'import socket; import re; print(re.sub("\..*", "", socket.gethostname()))'
    foobar
  [1]: http://pubs.opengroup.org/onlinepubs/9699919799.2018edition/functions/gethostname.html