import json from urllib.request import urlopen def get_my_country_code(): try: request = "https://geolocation-db.com/json/%" with urlopen(request) as url: data = json.loads(url.read().decode()) c_code = data['country_code'] if c_code == None: return 'KR' return c_code except: return 'KR' country_code = get_my_country_code() print(country_code)