views:

304

answers:

0

I'm trying to use urllib2 and python-ntlm to fetch an url behind and NTLM-authenticated proxy (although it should also suppoprt Basic and Digest authentication), and I'm getting a 407 HTTP error.

The code is:

    import urlib2
    from ntlm import HTTPNtlmAuthHandler
    from urlparse import urlparse, urlunparse

    user = { 'id' : 'my_user' , 'password' : 'my_password' }
    proxy = { 'type' : 'http' , 'host' : 'proxy_host', 'port' : proxy_port }

    # determine a base_uri for which the username and password can be used
    parsed_url = urlparse(url)
    base_uri = urlunparse((parsed_url[0],parsed_url[1],"","","",""))

    passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
    passman.add_password(None, base_uri, user['id'], user['password'])

    # create authentication handlers
    auth_NTLM = HTTPNtlmAuthHandler.HTTPNtlmAuthHandler(passman)
    auth_basic = urllib2.HTTPBasicAuthHandler(passman)
    auth_digest = urllib2.HTTPDigestAuthHandler(passman)

    # create proxy handler
    proxy_handler = urllib2.ProxyHandler({ proxy['type'] : proxy['host']+':'+str(proxy['port']) })

    # create and install the opener
    opener = urllib2.build_opener(proxy_handler, auth_NTLM, auth_digest, auth_basic)
    urllib2.install_opener(opener)

    urllib2.urlopen(url).read()

and i'm getting:

urllib2.HTTPError: HTTP Error 407: Proxy Authentication Required ( The ISA Server requires authorization to fulfill the request. Access to the Web Proxy filter is denied.  )

Any suggestions?