Last modified: Sep 13, 2026

Get External IP Python

When working with network applications in Python, you may often need to find out your public or external IP address. This is useful for logging, debugging, or tracking user locations. In this article, we will explore several methods to retrieve the external IP address using Python.

What Is an External IP Address?

An external IP address is the unique address assigned to your device by your Internet Service Provider (ISP). It allows other devices on the internet to identify and communicate with your system. Unlike a local IP address, which is used within your home or office network, the external IP is visible to the outside world.

Why Do You Need to Get External IP?

There are many reasons why developers need to retrieve the external IP address:

  • For logging access details in web applications.
  • To detect changes in network configuration.
  • For geolocation services based on IP.
  • To monitor and secure network traffic.

Method 1: Using the requests Library

The most common way to get your external IP is by making an HTTP request to a public service. We will use the popular requests library for this purpose.

First, install the library if you haven’t already:


pip install requests

Now, here’s how to get the external IP:


import requests

# Send a GET request to the API
response = requests.get('https://api.ipify.org')

# Print the external IP address
print("External IP:", response.text)

Output:


External IP: 123.45.67.89

This method uses the free API provided by ipify. It returns the IP as plain text, making it easy to use directly.

Method 2: Using JSON Response

Some APIs return the IP address in JSON format. This is useful if you want to parse additional metadata along with the IP.


import requests

# Request JSON response from the API
response = requests.get('https://ipinfo.io/json')

# Parse the JSON data
data = response.json()

# Print the external IP address
print("External IP:", data['ip'])

Output:


External IP: 123.45.67.89

This example uses the ipinfo.io API. It provides more information like city, region, and country along with the IP.

Method 3: Using the urllib Module

If you prefer not to install external packages, Python’s built-in urllib module can help. It is part of the standard library, so no installation is needed.


import urllib.request

# Open the URL and read the response
with urllib.request.urlopen('https://api.ipify.org') as response:
    ip = response.read().decode('utf-8')

# Print the external IP address
print("External IP:", ip)

Output:


External IP: 123.45.67.89

This method is lightweight and does not require any third-party libraries. It is ideal for simple scripts or environments where installing packages is restricted.

Method 4: Using the socket Module

While the socket module cannot directly fetch the external IP, it can be used in combination with a connection to an external server. However, this method is less reliable and not recommended for production use.


import socket

# Create a UDP socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

try:
    # Connect to an external server (does not send data)
    s.connect(('8.8.8.8', 80))
    # Get the local endpoint IP
    ip = s.getsockname()[0]
finally:
    s.close()

print("Local IP (via socket):", ip)

Output:


Local IP (via socket): 192.168.1.10

Note that this returns the local IP, not the external IP. For the external IP, stick to HTTP-based methods.

Handling Errors and Exceptions

Network requests can fail due to no internet connection or API downtime. Always wrap your code in a try-except block to handle errors gracefully.


import requests

try:
    # Attempt to get the external IP
    response = requests.get('https://api.ipify.org', timeout=5)
    response.raise_for_status()
    print("External IP:", response.text)
except requests.exceptions.RequestException as e:
    # Handle any request-related errors
    print("Error fetching IP:", e)

This ensures your program does not crash if the request fails.

Conclusion

Getting the external IP address in Python is straightforward with the right tools. Whether you use the requests library or the built-in urllib module, public APIs make it easy to retrieve this information. Always handle potential errors to make your application more robust. Try the examples above and choose the method that best fits your project.