Advertisement
Guest User

Untitled

a guest
Oct 2nd, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.13 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. # Notes From wxl:
  4. # I'm not sure this is sufficient to fix the problem, but some testing will help shed some light on it.
  5.  
  6. # Since it sounds like this is a common gotcha with urllib, this may very well be the issue.
  7. # My gut tells me timeouts, though, given the fact that it works sometimes and not others.
  8. # So I encourage you in a further commit to add an arbitrary timeout and a simple enough
  9. # error handling to perhaps eschew the need for a particular mirror
  10. # and instead use some default. @tsimonq2 what would be a good default?
  11.  
  12. # Then as bonus points, add error handling to handle other errors.
  13.  
  14. import json
  15. import urllib.request
  16.  
  17. def getcountry():
  18.     # This is hardcoded for now, but should eventually be put into the config
  19.     try:
  20.         with urllib.request.urlopen("https://ipapi.co/json", timeout=10) as url:
  21.             localedata = json.loads(url.read().decode())
  22.         return localedata["country"]
  23.     except:
  24.         return "US"
  25.  
  26. countryResult = getcountry()
  27. print('countryResult = ' + str(countryResult))
  28. print('type(countryResult) = ' + str(type(countryResult)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement