Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- # Notes From wxl:
- # I'm not sure this is sufficient to fix the problem, but some testing will help shed some light on it.
- # Since it sounds like this is a common gotcha with urllib, this may very well be the issue.
- # My gut tells me timeouts, though, given the fact that it works sometimes and not others.
- # So I encourage you in a further commit to add an arbitrary timeout and a simple enough
- # error handling to perhaps eschew the need for a particular mirror
- # and instead use some default. @tsimonq2 what would be a good default?
- # Then as bonus points, add error handling to handle other errors.
- # Error Handling Found Through StackOverFlow example:
- # https://stackoverflow.com/questions/12023135/python-3-errorhandling-urllib-requests
- import json
- import urllib.request
- def getcountry():
- # This is hardcoded for now, but should eventually be put into the config
- try:
- with urllib.request.urlopen("https://ipapi.co/json", timeout=10) as url: localedata = json.loads(url.read().decode())
- return localedata["country"]
- except urllib.error.URLError as e: print('http'); ResponseData = ''
- return "US"
- countryResult = getcountry()
- print('countryResult = ' + str(countryResult))
- print('type(countryResult) = ' + str(type(countryResult)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement