Advertisement
Guest User

Untitled

a guest
Oct 4th, 2018
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.32 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. # Error Handling Found Through StackOverFlow example:
  15. # https://stackoverflow.com/questions/12023135/python-3-errorhandling-urllib-requests
  16.  
  17. import json
  18. import urllib.request
  19.  
  20. def getcountry():
  21.     # This is hardcoded for now, but should eventually be put into the config
  22.     try:
  23.         with urllib.request.urlopen("https://ipapi.co/json", timeout=10) as url:            localedata = json.loads(url.read().decode())
  24.         return localedata["country"]
  25.     except urllib.error.URLError as e: print('http'); ResponseData = ''
  26.         return "US"
  27.  
  28. countryResult = getcountry()
  29. print('countryResult = ' + str(countryResult))
  30. print('type(countryResult) = ' + str(type(countryResult)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement