Menu Close

How to Get Public IP Address Using Python

Get Public IP Address Using Python

In today’s interconnected world, getting your public IP address can be essential for various tasks in Python, such as configuring servers, setting up VPNs, or debugging network issues. Fortunately, Python provides several straightforward methods to retrieve your public IP address.

In this article, we will explore a few popular techniques to Get Public IP Address Using Python.

What is a Public IP Address?

A public IP address is assigned to your network by your Internet Service Provider (ISP) and is visible to the outside world. This address is what other devices use to communicate with your device over the internet.

Get Public IP Address Using Python

Let’s see how to use Python to get a Public IP Address.

Method 1: Using Requests Library

One of the simplest methods to get your public IP address is by using the Python requests library to make a call to an external API that returns your IP.

Get Public IP Address Using Python
Get Public IP Address Using Python

If you don’t already have the requests library installed, you can do so using pip:

pip install requests

Here’s a basic Python code to fetch the Public IP Address using the Python requests library.

import requests

def get_public_ip():
    try:
        response = requests.get('https://api.ipify.org?format=json')
        ip_info = response.json()
        return ip_info['ip']
    except requests.RequestException as e:
        print(f"Error: {e}")
        return None

if __name__ == "__main__":
    public_ip = get_public_ip()
    if public_ip:
        print(f"Your public IP address is: {public_ip}")

After executing the above code, You can see your Public IP Address.

Method 2: Using socket and urllib

Another way to retrieve your public IP address is by utilizing the socket and urllib libraries. This method involves connecting to a known external service.

Here is the Python code to retrieve the Public IP Address using Python socket and urllib libraries.

import socket
import urllib.request

def get_public_ip():
    try:
        with urllib.request.urlopen('https://api64.ipify.org') as response:
            ip = response.read().decode('utf8')
            return ip
    except Exception as e:
        print(f"Error: {e}")
        return None

if __name__ == "__main__":
    public_ip = get_public_ip()
    if public_ip:
        print(f"Your public IP address is: {public_ip}")

Method 3: Using Third-Party Libraries

Several third-party libraries are available that simplify this process. One such library is ip-address.

Install the ip-address library via Python pip.

pip install ip-address

    This is the simple Python code to get Public IP Address using the ip-address library.

    import ip_address as ip
    
    if __name__ == "__main__":
        address = ip.get()
        print("Your public IP address is:", address)
    

    Conclusion

    Retrieving your public IP address in Python is a simple task with multiple methods to choose from. Whether you prefer using built-in libraries like requests, socket, and urllib, or a specialized third-party library,

    If you found this article helpful, Please share and keep visiting for further Python tips.

    Thanks for your time.

    This is written and verified by Vishvajit Rao.

    25+ Python While Loop Questions and Answers

    Related Posts