How to find the IP address of a system/website using Python

Posted in /  

How to find the IP address of a system/website using Python
vinaykhatri

Vinay Khatri
Last updated on December 22, 2024

    An IP address represents the physical address of our system or network. And when we connect to the internet or other networks, the server and other systems can identify our system using the IP address. Every website hosted on the internet has its unique IP address, although to access a website, we usually use its domain name. We can also pass its IP address on the browser URL bar to open the website.

    In this Python tutorial, I will walk you through the Python program on how to find the IP address of your own system and a website using its domain name. You can also find many online tools that can tell you the IP address of any website, but it would be cool to know how you can do that using Python. Before we dive into the implementation of our code, let’s discuss the library that we will be using for this tutorial.

    Python socket Library

    As the library name suggests, socket is the python de-facto socket Programming library. Using this library, we can connect our systems to different networks or create a set of networks for our local system. It is a very powerful library and is often used in ethical hacking and networking with Python.

    In this tutorial, I will be using the socket library to find the ip address of my system and ip address of the website using the website domain name. It is a Python standard library, so we do not need to install it. That’s all about this library now open your best Python IDE or Text editor and code along.

    How to find the IP address of your system

    Let’s begin with finding the IP address of our own system. First, we will import the socket library in our script using the import statement.

    import socket

    Now let’s define find_my_ip() Python user-defined function that will return the IP address of our system when called.

    def find_my_ip():
        my_name = socket.gethostname()
        my_ip_address = socket.gethostbyname(my_name)
    
        return my_ip_address

    The socket . gethostname () function will return the name of the host(system). In our case, it will return the user name of the system. The socket . gethostbyname () function will return the IP address of the specified hostname. In the above code, I have specified my_name as the hostname, so it will return the IP address of my system. Now let’s call the find_my_ip() function and print it.

    print("Your IP address is:")
    
    print(find_my_ip())

    Put all the code together and execute

    Python program to find the system IP address

    import socket
    
    def find_my_ip():
        my_name = socket.gethostname()
        my_ip_address = socket.gethostbyname(my_name)
        return my_ip_address
    
    print("Your IP address is:")
    print(find_my_ip())

    Output

    Your IP address is:
    198.83.34.1

    How to find the IP address of a website using DNS

    Now we know how to find the IP address of our system, let’s write a Python program that can find the IP address of a website when the user enters the website Domain Name. Similar to the above program, we will begin with importing the socket module in our script

    import socket

    Now let’s define a Python user-defined function def find_web_ip(domain_name) that accepts the domain name and returns its IP address.

    def find_web_ip(domain_name):
        domain_ip = socket.gethostbyname(domain_name)
        return domain_ip

    The gethostbyname() function accepts the domain name and returns its IP address. Now let’s ask the user to enter the domain name, call the find_web_ip() function and print the returned IP address.

    domain_name = input("Enter Domain name eg: www.eg.com: ")
    
    print(f"The IP address of {domain_name} is: ")
    
    print(find_web_ip(domain_name))

    Now put all the code together and execute

    Python program to find the IP address of a website using its domain name

    import socket
    def find_web_ip(domain_name):
        domain_ip = socket.gethostbyname(domain_name)
        return domain_ip
    
    domain_name = input("Enter Domain name eg: www.eg.com: ")
    print(f"The IP address of {domain_name} is: ")
    print(find_web_ip(domain_name))

    Output

    Enter Domain name eg: www.eg.com: www.google.com
    The IP address of www.google.com is:
    172.217.163.196

    In the output, we specified the domain name of www.google.com , and in return, I received its IP address 172.217.163.196 . And you can also type the IP address 172.217.163.196 on your browser URL bar it will open google.com.

    Conclusion

    Now let’s wrap up the article. In this Python tutorial, you learned how you could find the IP address of your system and the website using its domain name. The Python socket library makes it very easy to work with networks and IP addresses. It took only one socket function gethostbyname() to find the ip addresses of our system and goolge.com.

    There are many cool things we can do with the Python socket library, such as find open ports using Python or make a server-client chat room .

    People are also reading:

    Leave a Comment on this Post

    0 Comments