From within a Python script I am trying to get the host name in a Linux box. It is a Debian GNU/Linux Amazon EC2 instance. I have set the correct name in /etc/hostname. The recommended solution socket.gethostname() is not working: it shows the ip- plus the IP tuple.
I have searched on StackOverflow and nothing is coming out, e.g. here. socket.getfqdn() is even worse: it yields ip-[IP tuple].eu-west-1.compute.internal.
Am I doing something wrong, or is there no clean solution to get the hostname in /etc/hostname? Of course the back-up solution is to read the file etc/hostname itself, but something so inherently platform-dependent is somehow too aberrant. Thanks!
socket.gethostname()returns the correct hostname. So the problem was just that the machine had not been rebooted and it had not taken the new hostname. If you want to answer the question with this info I will upvote it.