Hi, I as a Python novice I throw myself at the mercy of experts in this forum. Could someone post a simple example of how to implement urllib2's proxy authentication. I've read all the references google returns as well as searched this site and cannot find an example that I can understand about how to do this.
I can do the simple proxy handler (below) as outlined on the Missing Manual but still get 407 errors because I don't know how to do the authentication part.

proxy_support = urllib2.ProxyHandler({})
opener = urllib2.build_opener(proxy_support)
urllib2.install_opener(opener)

I'd be massively grateful for any help - I can't imagine I am alone in trying to understand this.

Dani AI

Generated

Short version: a proxy mapping alone (what suggested) only tells urllib2 where the proxy lives — a 407 means that proxy also wants credentials. Use a password manager plus a proxy auth handler so urllib2 can respond to the proxy's challenge.

A minimal Python 2 example (uses urllib2's password manager + ProxyBasicAuthHandler):

import urllib2

proxy = 'http://proxy.example.com:8080'
user = 'myuser'
pw = 'mypass'

proxy_handler = urllib2.ProxyHandler({'http': proxy})
pwd_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
pwd_mgr.add_password(None, proxy, user, pw)         # realm=None matches any realm
proxy_auth = urllib2.ProxyBasicAuthHandler(pwd_mgr)

opener = urllib2.build_opener(proxy_handler, proxy_auth)
urllib2.install_opener(opener)

resp = urllib2.urlopen('http://example.com/')
print resp.read()

Notes and troubleshooting

  • If the proxy uses Digest instead of Basic, use ProxyDigestAuthHandler; for NTLM/Kerberos you will need third‑party support (e.g. python-ntlm or requests+requests-ntlm) because urllib2 does not handle those out of the box.
  • Avoid putting credentials in plain URLs in shared code (e.g. ) — it can leak in logs and is less flexible.
  • To debug, add HTTPHandler/HTTPSHandler with debuglevel=1 to your opener to see the raw request/response and the proxy's Proxy-Authenticate header. That header tells you which auth scheme (Basic/Digest/NTLM) the proxy requires.
  • If you still get 407 after wiring the handlers, capture the proxy's 407 response (Proxy-Authenticate value) and post it here; also state whether this is a corporate (NTLM) proxy or a simple Basic/Digest proxy.

This approach keeps auth separate from the proxy mapping and is the reliable way to satisfy proxy challenges with urllib2.

Recommended Answers

All 2 Replies

You need to put

{"http" : ""}

inside the proxyhandler's argument

Hi, thanks, but I have tried that and still get the following error

HTTPError: HTTP Error 407: Proxy Authentication Required
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.