Python has a built-in function to convert hostname to
IP addresses without having to use external program.
It is provided by the
socket module and you can use the
gethostbyname function within the module to resolve a hostname to an
Steps to get IP address from Hostname in Python
$ ipython3 Python 3.8.2 (default, Apr 27 2020, 15:53:34) Type 'copyright', 'credits' or 'license' for more information IPython 7.13.0 -- An enhanced Interactive Python. Type '?' for help.
In : import socket
IPfrom hostname by calling
socket.gethostbynamefunction with hostname as parameter .
In : socket.gethostbyname('www.google.com') Out: '220.127.116.11'
Hostname in this context refers to
Fully Qualified Domain Name or
Pythonscript that accepts
hostnameas parameter and outputs
IPaddress such as the following.
#!/usr/bin/env python3 import socket import sys hostname = sys.argv ip = socket.gethostbyname(hostname) print('Hostname: ', hostname, '\n' 'IP: ', ip)
Comment anonymously. Login not required.