Python Glossary Русский

Python 3.4+urllib.requestterm

urllib.request.DataHandler

protocol handlers

The handler for data: URLs — data written right into the address (RFC 2397). It needs no network and no files.

Syntax

urllib.request.urlopen('data:[<mediatype>][;base64],<data>')

It is handy for testing code built on urlopen(): the response is written inline as a string. Without ';base64' the content is percent-encoded, with it base64; when no type is given, text/plain;charset=US-ASCII is assumed. The response is still bytes, and resp.status is None.

Examples

import urllib.request
with urllib.request.urlopen('data:;base64,SGk=') as resp:
    print(resp.read(), resp.headers['Content-type'])
# → b'Hi' text/plain;charset=US-ASCII

See also

Open in the full glossarydocs.python.orgReport an inaccuracy