httpbin.org is a free HTTP request and response service that provides a range of APIs, including the ability to obtain a public IP address.
By accessing httpbin.org/ip, you can get your public IP address. The response it returns is in JSON format, which includes information about your public IP address.
There are several advantages to using httpbin.org's free IP address service:
Free: httpbin.org's IP address service is provided free of charge, allowing you to obtain your public IP address at any time without paying any fees.
Easy to use: Simply send an HTTP GET request to httpbin.org/ip to get your public IP address, without the need for complex configuration or authentication processes.
High reliability: httpbin.org is a stable and reliable service, with its APIs consistently available and providing good response speed.
import requests
import time
def get_public_ip():
response = requests.get('https://httpbin.org/ip')
ip_info = response.json()
return ip_info['origin']
def get_city_from_ip(ip):
url = f'http://ip-api.com/json/{ip}?fields=city'
response = requests.get(url)
if response.status_code == 200:
data = response.json()
return data.get('city', 'Unknown City')
else:
return 'Unable to retrieve city information'
if __name__ == '__main__':
public_ip = get_public_ip()
print(f'Public IP: {public_ip}')
city = get_city_from_ip(public_ip)
print(f'City: {city}')
while True:
time.sleep(1)
Graphical version:
import requests
from tkinter import Tk, Label, Button
from tkinter import ttk
def get_public_ip():
try:
response = requests.get('https://httpbin.org/ip')
ip_info = response.json()
return ip_info['origin']
except requests.RequestException as e:
return str(e)
def get_city_from_ip(ip):
try:
url = f'http://ip-api.com/json/{ip}?fields=city'
response = requests.get(url)
if response.status_code == 200:
data = response.json()
return data.get('city', 'Unknown City')
else:
return 'Unable to retrieve city information'
except requests.RequestException as e:
return str(e)
def update_city():
public_ip = get_public_ip()
city = get_city_from_ip(public_ip)
ip_label.config(text=f'Public IP: {public_ip}')
city_label.config(text=f'City: {city}')
# Create main window
root = Tk()
root.title("Get Public IP and City")
root.geometry("450x150")
# Create labels
ip_label = Label(root, text="Public IP: Waiting for retrieval...")
city_label = Label(root, text="City: Waiting for retrieval...")
ip_label.pack(pady=10)
city_label.pack(pady=10)
# Create button
style = ttk.Style()
style.configure("TButton", font=("Arial", 12))
get_button = ttk.Button(root, text="Get IP and City", command=update_city, style="TButton")
get_button.pack(pady=10)
# Run main loop
root.mainloop()