Handle network exceptions

This commit is contained in:
2017-11-18 14:58:46 +00:00
parent 81dae53d65
commit 21866c38a5
3 changed files with 16 additions and 14 deletions

View File

@@ -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('')