Python Glossary Русский

all Python 3urllib.requestterm

urllib.request.HTTPPasswordMgr

authentication

A password store for authentication handlers: add_password(realm, uri, user, passwd) to record, lookup by realm and address.

Syntax

mgr = urllib.request.HTTPPasswordMgr(); mgr.add_password(realm, uri, user, passwd)

The trap is the realm: it must match what the server sent in WWW-Authenticate, otherwise the password is not found and you get HTTPError 401. When the realm is unknown in advance, use HTTPPasswordMgrWithDefaultRealm with realm=None. The address is matched by prefix: a password for http://site/ also covers http://site/api, but not another host.

Examples

import urllib.request
mgr = urllib.request.HTTPPasswordMgr()
mgr.add_password('api', 'http://example.com/', 'user', 'secret')
print(mgr.find_user_password('api', 'http://example.com/data'))  # → ('user', 'secret')
print(mgr.find_user_password(None, 'http://example.com/data'))  # → (None, None)

See also

Open in the full glossarydocs.python.orgReport an inaccuracy