Autotesting_Selenium/get_region_by_ip.py

25 lines
651 B
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import http.client
conn = http.client.HTTPConnection("ifconfig.me")
conn.request("GET", "/ip")
ip = conn.getresponse().read()
fixed = ip.decode("utf-8").strip()
# print (fixed)
import requests
def get_user_region(ip):
try:
response = requests.get(f"https://ipinfo.io/{ip}/json?lang=ru")
data = response.json()
return data.get("region")
except Exception as e:
print(f"Произошла ошибка: {str(e)}")
return None
user_region = get_user_region(fixed)
if user_region:
print(f"Ваш регион: {user_region}")
else:
print("Не удалось определить регион.")