[Python-ideas] Support data: URLs in urllib
Mathias Panzenböck
grosser.meister.morti at gmx.net
Wed Oct 31 06:02:52 CET 2012
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
More information about the Python-ideas
mailing list