Proxy Authentication using urllib2

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Andre Bocchini

    Proxy Authentication using urllib2

    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 HTTPBasicAuthHa ndler:

    # set up authentication info
    authinfo = urllib2.HTTPBas icAuthHandler()
    authinfo.add_pa ssword('realm', 'host', 'username', 'password')
    proxy_support = urllib2.ProxyHa ndler({"http" : "http://ahad-haam:3128"})
    # build a new opener that adds authentication and caching FTP handlers
    opener = urllib2.build_o pener(proxy_sup port, authinfo,
    urllib2.CacheFT PHandler)
    # 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.ProxyHa ndler({"http" : "http://myproxy:3128"})
    proxy_auth_hand ler = urllib2.ProxyBa sicAuthHandler( )
    proxy_auth_hand ler.add_passwor d(None, "myproxy", "myname", "mypass")
    opener = urllib2.build_o pener(proxy_han dler, proxy_auth_hand ler)
    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


  • John J. Lee

    #2
    Re: Proxy Authentication using urllib2

    Andre Bocchini <lists@andreboc chini.com> writes:
    [color=blue]
    > 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 HTTPBasicAuthHa ndler:[/color]
    [...][color=blue]
    > proxy_handler = urllib2.ProxyHa ndler({"http" : "http://myproxy:3128"})
    > proxy_auth_hand ler = urllib2.ProxyBa sicAuthHandler( )
    > proxy_auth_hand ler.add_passwor d(None, "myproxy", "myname", "mypass")
    > opener = urllib2.build_o pener(proxy_han dler, proxy_auth_hand ler)
    > urllib2.install _opener(opener)
    > f = urllib2.urlopen ("http://www.python.org" )
    > data = f.readlines()[/color]

    Can't see anything wrong with that.

    [color=blue]
    > I always get a 407 error.[/color]

    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




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


    John

    Comment

    • Myles

      #3
      Re: Proxy Authentication using urllib2

      Andre Bocchini <lists@andreboc chini.com> wrote in message news:<mailman.1 064015428.25301 .python-list@python.org >...[color=blue]
      > 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[/color]

      A piece of code that works through a Squid proxy here - originally
      clipped from comp.lang.pytho n - 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.ProxyHa ndler({"http" : \
      "http://%(user)s:%(pass )s@%(host)s:%(p ort)d" % proxy_info})
      opener = urllib2.build_o pener(proxy_sup port, urllib2.HTTPHan dler)

      # install it
      urllib2.install _opener(opener)

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

      Regards, Myles.

      Comment

      Working...