Get IP address of url in python?

36,289

Solution 1

>>> import socket
>>> print socket.gethostbyname('google.com')
74.125.236.52

Solution 2

You can use gethostbyname_ex() function located in socket module http://docs.python.org/library/socket.html

Share:
36,289
Stian
Author by

Stian

Updated on December 04, 2020

Comments

  • Stian
    Stian over 3 years

    Possible Duplicate:
    How can I do DNS lookups in Python, including referring to /etc/hosts?

    Im using Python 2.7 and im creating a iptracer with an online API.

    And i want the user to have the option to type e.g google.com, http://google.com or www.google.com and then get the ip address of that and add the ip to a variable.

    Is it any links or other help anyone can give me?

    -stian

  • aderchox
    aderchox over 4 years
    On Python 3.7 it doesn't work: socket.gaierror: [Errno -2] Name or service not known
  • volkoshkursk
    volkoshkursk almost 4 years
    @aderchox on python 3.7 I've successfully run following: import socket print(socket.gethostbyname('google.com'))