Python Glossary Русский

all Python 3urllib.requestfunction

urllib.request.install_opener()

openers

Makes the given opener global: after the call every urlopen() in the program goes through its handlers.

Syntax

urllib.request.install_opener(opener)

The swap is global: it also affects third-party libraries that call urlopen(), which is handy in a script and dangerous in a library, where calling opener.open() directly is better. There is no type check: any object with an open() method will do. urlcleanup() restores the default behaviour.

Examples

import urllib.request
class Spy(urllib.request.BaseHandler):
    def data_response(self, req, resp):
        print('через мой opener:', req.full_url)  # → через мой opener: data:,hi
        return resp
urllib.request.install_opener(urllib.request.build_opener(Spy))
with urllib.request.urlopen('data:,hi') as resp:
    print(resp.read())  # → b'hi'

See also

Open in the full glossarydocs.python.orgReport an inaccuracy