all Python 3urllib.requestterm
urllib.request.ProxyDigestAuthHandler
authentication
Digest authentication against a proxy: it answers a 407 with Proxy-Authenticate: Digest and repeats the request with a hashed response.
Syntax
urllib.request.ProxyDigestAuthHandler(password_mgr=None)
The counterpart of HTTPDigestAuthHandler for proxies: code 407 and the Proxy-Authorization header instead of 401 and Authorization. The password manager looks the entry up by the proxy address, not the site's — a common reason why the password is not sent. A Basic proxy needs ProxyBasicAuthHandler; Digest does not replace it.
Examples
import email.message, io, urllib.error, urllib.request, urllib.response
def reply(req, code, body=b'', **headers):
msg = email.message.Message()
for name, value in headers.items():
msg[name.replace('_', '-')] = value
resp = urllib.response.addinfourl(io.BytesIO(body), msg, req.full_url, code)
resp.msg = 'demo'
return resp
class Proxy(urllib.request.BaseHandler):
handler_order = 100
def http_open(self, req):
auth = req.get_header('Proxy-authorization')
if auth:
print(auth[:17]) # → Digest username="
return reply(req, 200, b'ok')
return reply(req, 407, Proxy_Authenticate='Digest realm="proxy", nonce="n1", qop="auth"')
mgr = urllib.request.HTTPPasswordMgrWithDefaultRealm()
mgr.add_password(None, 'example.com', 'u', 'p')
opener = urllib.request.build_opener(Proxy, urllib.request.ProxyDigestAuthHandler(mgr))
print(opener.open('http://example.com/').read()) # → b'ok'
See also
Open in the full glossarydocs.python.orgReport an inaccuracy