Error getting server's IP address using Python on CentOS

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.

Python Novice

Posted 2014-04-04T21:53:30.667

Reputation: 103

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 – 2014-04-04T22:13:53.963

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 – 2014-04-05T02:31:56.610

Answers

0

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.

Python Novice

Posted 2014-04-04T21:53:30.667

Reputation: 103