From 52e5198d88720ac0814935b0b1707d99c46c1f3c Mon Sep 17 00:00:00 2001 From: Hal Simpson <hasimpson@usgs.gov> Date: Fri, 28 Aug 2015 14:05:32 -0600 Subject: [PATCH] Added read_url to Util file --- geomagio/Util.py | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/geomagio/Util.py b/geomagio/Util.py index 85eee183..028d0339 100644 --- a/geomagio/Util.py +++ b/geomagio/Util.py @@ -1,4 +1,4 @@ - +import urllib2 class ObjectView(object): """ @@ -17,3 +17,33 @@ class ObjectView(object): Override string representation to output wrapped dictionary. """ return str(self.__dict__) + + +def read_url(url): + """Open and read url contents. + + Parameters + ---------- + url : str + A urllib2 compatible url, such as http:// or file://. + + Returns + ------- + str + contents returned by url. + + Raises + ------ + urllib2.URLError + if any occurs + """ + response = urllib2.urlopen(url) + content = None + try: + content = response.read() + except urllib2.URLError, e: + print e.reason + raise + finally: + response.close() + return content -- GitLab