+def getContent(url='',stdin=False):
+ '''Generic content retriever, DO NOT use this function in a CGI script as
+ it can read from the local disk (which you probably don't want it to).
+ '''
+
+ encoding = '' # If we don't populate this, the script will assume UTF-8
+
+ # Special case, if this is a HTTP url, return the data from it using
+ # the HTTP functions which attempt to play a bit nicer.
+ parsedURL = urlparse.urlparse(url)
+ if 'http' in parsedURL[0]: return getHTTPContent(url)
+
+ if stdin:
+ content = sys.stdin.read()
+ return (content, encoding)
+
+ if not parsedURL[0]: url = 'file://' + os.path.abspath(url)
+
+ # If we've survived, use python's generic URL opening library to handle it
+ import urllib2
+ try:
+ res = urllib2.urlopen(url)
+ content = res.read()
+ ct = res.info().getplist()
+ res.close()
+ except (urllib2.URLError, OSError), e:
+ sys.stderr.write('%s\n'%e)
+ sys.exit(1)
+
+ for param in ct:
+ if 'charset' in param:
+ encoding = param.split('=')[1]
+ break