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

@@ -2,7 +2,9 @@
import requests import requests
try:
r = requests.get('https://blockchain.info/ticker'); r = requests.get('https://blockchain.info/ticker');
ticker = r.json() ticker = r.json()
print('£%.2f' % float(ticker['GBP']['sell'])) print('£%.2f' % float(ticker['GBP']['sell']))
except requests.exceptions.RequestException as e:
sys.exit(1)

View File

@@ -4,12 +4,12 @@ from xml.etree import ElementTree as ET
url = 'http://freegeoip.net/xml/' url = 'http://freegeoip.net/xml/'
try:
with urlopen(url) as res: with urlopen(url) as res:
s = res.read() s = res.read()
root = ET.fromstring(s) root = ET.fromstring(s)
ip = root.find('IP').text ip = root.find('IP').text
country = root.find('CountryName').text country = root.find('CountryName').text
print('%s (%s)' % (ip, country)) print('%s (%s)' % (ip, country))
except Exception:
print('')

View File

@@ -3,5 +3,5 @@ import subprocess
import re import re
url = 'google.co.uk' url = 'google.co.uk'
ps = subprocess.Popen('ping -c 1 %s | tail -1 | awk \'{print $4}\' | cut -d \'/\' -f 2' % url, shell=True, stdout=subprocess.PIPE) ps = subprocess.Popen('ping -c 1 %s | tail -1' % url, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
print(re.sub(r'[^0-9.+]', '', str(ps.stdout.read())).split('.')[0]) print(re.sub(r'[^0-9.+]', '', str(ps.stdout.read())).split('.')[0])