Python Glossary Русский

all Python 3urllib.requestfunction

urllib.request.urlcleanup()

opening URLs

Removes temporary files left behind by urlretrieve() and resets the global opener installed by install_opener().

Syntax

urllib.request.urlcleanup()

It does two things, and the second one is a surprise: afterwards urlopen() goes back to the standard handler set, and your opener with its headers and cookies is gone. Files that urlretrieve() saved under a name you gave are left alone — only the temporary ones created without filename are removed.

Examples

import io, urllib.error, urllib.request, urllib.response
class Demo(urllib.request.BaseHandler):
    def demo_open(self, req):
        return urllib.response.addinfourl(io.BytesIO(b'ok'), {}, req.full_url)
urllib.request.install_opener(urllib.request.build_opener(Demo))
print(urllib.request.urlopen('demo://x').read())  # → b'ok'
urllib.request.urlcleanup()
try:
    urllib.request.urlopen('demo://x')
except urllib.error.URLError as e:
    print(e.reason)  # → unknown url type: demo

See also

Open in the full glossarydocs.python.orgReport an inaccuracy