
Oct. 31, 2012
5:02 a.m.
Sometimes it would be handy to read data:-urls just like any other url. While it is pretty easy to parse a data: url yourself I think it would be nice if urllib could do this for you. Example data url parser:
import base64 import urllib.parse
def read_data_url(url): scheme, data = url.split(":") assert scheme == "data", "unsupported scheme: "+scheme mimetype, data = data.split(",") if mimetype.endswith(";base64"): return mimetype[:-7] or None, base64.b64decode(data.encode("UTF-8")) else: return mimetype or None, urllib.parse.unquote(data).encode("UTF-8")
See also: http://tools.ietf.org/html/rfc2397 -panzi