Monitor your public IP address using Python

Melanee Group
1 min readMay 30, 2023

--

Monitor your public IP address using Python
Photo by Sigmund on Unsplash

A few days ago, one of my friends told me, he needs a program that can monitor his public IP to check if his VPN is connected or not.

He needs this program for his job. The company that he works for, calculates his salary according to how much he was connected to this VPN.

Finally, I came across this Python program for him:


import requests
import time

def get_public_ip():
response = requests.get('https://api.ipify.org').text
return response

interval = 5 # Interval in seconds
while True:
public_ip = get_public_ip()
print(f'Public IP: {public_ip}')
time.sleep(interval)

These few lines of Python code can monitor your public IP every 5 seconds. If your public IP has been changed you can see the changes.

Alternative uses

There are number of use cases for this code, mine was checking if my computer is still connected to a VPN. Another use case is to implement this code in the back end of websites to check IP of clients and see where are they from and which country and language they use.

Author:

Melanee

--

--

Melanee Group
Melanee Group

No responses yet