Handle network exceptions
This commit is contained in:
@@ -4,12 +4,12 @@ from xml.etree import ElementTree as ET
|
||||
|
||||
url = 'http://freegeoip.net/xml/'
|
||||
|
||||
with urlopen(url) as res:
|
||||
s = res.read()
|
||||
|
||||
root = ET.fromstring(s)
|
||||
|
||||
ip = root.find('IP').text
|
||||
country = root.find('CountryName').text
|
||||
|
||||
print('%s (%s)' % (ip, country))
|
||||
try:
|
||||
with urlopen(url) as res:
|
||||
s = res.read()
|
||||
root = ET.fromstring(s)
|
||||
ip = root.find('IP').text
|
||||
country = root.find('CountryName').text
|
||||
print('%s (%s)' % (ip, country))
|
||||
except Exception:
|
||||
print('')
|
||||
|
||||
Reference in New Issue
Block a user