Proxy Authentication using urllib2

Discussion in 'Python' started by Andre Bocchini, Sep 20, 2003.

  1. I'm having some trouble using proxy authentication. I can't figure out
    how to authenticate with a Squid proxy. I know for a fact the proxy is
    using Basic instead of Digest for the authentication. I can
    authenticate just fine using Mozilla. I've done some Google searches,
    but the closest piece of code I've is is for HTTPBasicAuthHandler:

    # set up authentication info
    authinfo = urllib2.HTTPBasicAuthHandler()
    authinfo.add_password('realm', 'host', 'username', 'password')
    proxy_support = urllib2.ProxyHandler({"http" : "http://ahad-haam:3128"})
    # build a new opener that adds authentication and caching FTP handlers
    opener = urllib2.build_opener(proxy_support, authinfo,
    urllib2.CacheFTPHandler)
    # install it
    urllib2.install_opener(opener)
    f = urllib2.urlopen('http://www.python.org/')

    Can anybody point me in the right direction to some more detailed
    documentation? I haven't really been able to understand what I found
    in the Python Library Reference either.

    I thought something like this might work, but is didn't:

    proxy_handler = urllib2.ProxyHandler({"http" : "http://myproxy:3128"})
    proxy_auth_handler = urllib2.ProxyBasicAuthHandler()
    proxy_auth_handler.add_password(None, "myproxy", "myname", "mypass")
    opener = urllib2.build_opener(proxy_handler, proxy_auth_handler)
    urllib2.install_opener(opener)
    f = urllib2.urlopen("http://www.python.org")
    data = f.readlines()

    I always get a 407 error.

    Well, thanks in advance.

    -- Andre
    Andre Bocchini, Sep 20, 2003
    #1
    1. Advertising

  2. Andre Bocchini

    John J. Lee Guest

    Andre Bocchini <> writes:

    > I'm having some trouble using proxy authentication. I can't figure
    > out how to authenticate with a Squid proxy. I know for a fact the
    > proxy is using Basic instead of Digest for the authentication. I can
    > authenticate just fine using Mozilla. I've done some Google searches,
    > but the closest piece of code I've is is for HTTPBasicAuthHandler:

    [...]
    > proxy_handler = urllib2.ProxyHandler({"http" : "http://myproxy:3128"})
    > proxy_auth_handler = urllib2.ProxyBasicAuthHandler()
    > proxy_auth_handler.add_password(None, "myproxy", "myname", "mypass")
    > opener = urllib2.build_opener(proxy_handler, proxy_auth_handler)
    > urllib2.install_opener(opener)
    > f = urllib2.urlopen("http://www.python.org")
    > data = f.readlines()


    Can't see anything wrong with that.


    > I always get a 407 error.


    Are you sure you don't need a realm? Try sniffing what Mozilla and
    Python are sending (using eg. ethereal). You *should* get a 407
    response, IIRC, but then urllib2 should respond with an appropriate
    Proxy-authorization header.

    If that helped, please look at the doc patch here

    http://www.python.org/sf/798244


    test it, and post a comment to say whether or not it worked (and which
    examples you tried -- preferably all of them ;).


    John
    John J. Lee, Sep 21, 2003
    #2
    1. Advertising

  3. Andre Bocchini

    Myles Guest

    Andre Bocchini <> wrote in message news:<>...
    > I'm having some trouble using proxy authentication. I can't figure out
    > how to authenticate with a Squid proxy. I know for a fact the proxy is


    A piece of code that works through a Squid proxy here - originally
    clipped from comp.lang.python - possibly an eff-bot post if my dim
    memory serves me correctly.

    ---- snip ----
    import urllib2

    proxy_info = {
    'user' : 'username',
    'pass' : 'password',
    'host' : "proxy.name.com",
    'port' : 80 # or 8080 or whatever
    }

    # build a new opener that uses a proxy requiring authorization
    proxy_support = urllib2.ProxyHandler({"http" : \
    "http://%(user)s:%(pass)s@%(host)s:%(port)d" % proxy_info})
    opener = urllib2.build_opener(proxy_support, urllib2.HTTPHandler)

    # install it
    urllib2.install_opener(opener)

    # use it
    f = urllib2.urlopen('http://www.python.org/')
    print f.headers
    print f.read()
    ---- snip ----

    Regards, Myles.
    Myles, Sep 23, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Herman Geldenhuys

    % in POST when using URLLIB2.URLOPEN with PROXY

    Herman Geldenhuys, Jul 14, 2004, in forum: Python
    Replies:
    1
    Views:
    456
    John J. Lee
    Jul 14, 2004
  2. Josef Cihal
    Replies:
    0
    Views:
    726
    Josef Cihal
    Sep 5, 2005
  3. Ray Slakinski
    Replies:
    1
    Views:
    737
    John J. Lee
    Nov 8, 2005
  4. Licheng Fang
    Replies:
    2
    Views:
    509
    Jarek Zgoda
    Feb 19, 2006
  5. looping
    Replies:
    4
    Views:
    1,169
    John J. Lee
    Sep 8, 2006
Loading...

Share This Page