0

I'm using Python's socket module to get the server's IP address

server_ip = socket.gethostbyname(socket.getfqdn())

I get this error: socket.gaierror: [Errno -2] Name or service not known

Weird cause it works just fine on Fedora. Any ideas what's up?

Edit: The problem was the IP address could not be resolved to the hostname since it was not in /etc/hosts. Works fine now.

  • What's the rest of your code look like? Is a service already using the port you are trying to open? Run netstat -an | egrep 'Proto|LISTEN' to check – Bert Apr 4 '14 at 22:13
  • No, a service isn't running on that port. I'm just trying to do the equivalent of ifconfig | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}' in Python without using subprocess. – Python Novice Apr 5 '14 at 2:31
1

The problem was the IP address could not be resolved to the hostname since it was not in /etc/hosts nor was it resolvable by DNS. Since I was testing on a virtual machine, echo "X.X.X.X FQDN" >> /etc/hosts did the trick.

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.