Last modified: March 10, 2021

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 = 'https://ipinfo.io/json'
    response = requests.get(endpoint, verify = True)

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

    data = response.json()

    return data['ip']

#get my ip
my_ip = get()

#print my ip
print(my_ip)

Output


105.154.233.88