Last modified: Jan 10, 2023 By Alexander Williams

Get my Public IP in Python

In this tutorial, I'll show you how to get your public IP by using ipinfo API.

Get my public ip Example

Let's write a simple function that gets my public IP.

import requests
import json

def get():
    endpoint = ''
    response = requests.get(endpoint, verify = True)

    if response.status_code != 200:
        return 'Status:', response.status_code, 'Problem with the request. Exiting.'

    data = response.json()

    return data['ip']

#get my ip
my_ip = get()

#print my ip