How to get IP from hostname in Python + code

If you want to get the IP from a hostname in Python, you can use the socket module which is already included in Python. The next thing which you will need, is a variable that will hold the hostname which you want to request.

The second variable is the address variable. The IPHOLDER variable will hold the IP which you received by using socket.gethostbyname(hostname)

The print line will print out the found IP address.

get IP from hostname in Python

import socket
hostname = ''
IPHOLDER = socket.gethostbyname(hostname)
print '[ADDRESS] ', hostname, ' - ', IPHOLDER
Share This Message