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 hostname to IP address.
You can resolve hostname to IP address in Python by using the interactive shell or by writing a script.
$ 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
In : socket.gethostbyname('www.google.com') Out: '188.8.131.52'
Hostname in this context refers to Fully Qualified Domain Name or FQDN.
#!/usr/bin/env python3 import socket import sys hostname = sys.argv ip = socket.gethostbyname(hostname) print('Hostname: ', hostname, '\n' 'IP: ', ip)
$ python3 hostname-to-ip.py 'www.google.com' Hostname: www.google.com IP: 184.108.40.206
Comment anonymously. Login not required.