HTTPBasicAuthHandler doesn't work

Discussion in 'Python' started by nicolas.surribas@gmail.com, Jul 9, 2006.

  1. Guest

    Hi !
    I'm trying to add the HTTP basic authentification to my web spider but
    it doesn't work...
    The HTTPBasicAuthHandler don't send the headers for authentification
    :-(

    Here is the code : http://devloop.lyua.org/releases/lswww_urllib2.py

    def
    __init__(self,rooturl,firsturl=[],forbidden=[],proxy={},cookie="",auth_basic=[]):
    root=rooturl
    self.excluded=forbidden
    self.proxy=proxy
    self.cookie=cookie
    self.auth_basic=auth_basic
    if root[-1]!="/":
    root+="/"
    if(self.checklink(root)):
    print "Invalid link argument"
    sys.exit(0)
    for lien in firsturl:
    if(self.checklink(lien)):
    print "Invalid link argument"
    sys.exit(0)
    server=(root.split("://")[1]).split("/")[0]
    self.root=root
    self.server=server
    director = urllib2.OpenerDirector()

    director.add_handler(urllib2.HTTPHandler())
    director.add_handler(urllib2.HTTPSHandler())

    if self.proxy!={}:
    director.add_handler(urllib2.ProxyHandler(self.proxy))

    if self.auth_basic!=[]:
    auth=urllib2.HTTPBasicAuthHandler(urllib2.HTTPPasswordMgrWithDefaultRealm())
    auth.add_password(None, self.root, self.auth_basic[0],
    self.auth_basic[1])
    director.add_handler(auth)

    if self.cookie!="":
    cj = cookielib.LWPCookieJar()
    if os.path.isfile(self.cookie):
    cj.load(self.cookie,ignore_discard=True)
    director.add_handler(urllib2.HTTPCookieProcessor(cj))

    urllib2.install_opener(director)

    Where is the problem ?
    Thanks !
     
    , Jul 9, 2006
    #1
    1. Advertising

  2. John J. Lee Guest

    writes:

    > Hi !
    > I'm trying to add the HTTP basic authentification to my web spider but
    > it doesn't work...
    > The HTTPBasicAuthHandler don't send the headers for authentification
    > :-(


    Hi

    Several bugs were fixed with Basic auth in Python 2.5. I'd be most
    grateful if you can verify that your program works with Python 2.5
    beta1:

    http://www.python.org/download/releases/2.5/


    Please let me know either way whether or not it works for you with the
    2.5 beta1 (beta2 is coming very soon, so get in quick if you want to
    help make sure this works right in future!).

    Below are a couple of tips unrelated to your question.


    By the way, s/authentification/authentication/ :)


    > Here is the code : http://devloop.lyua.org/releases/lswww_urllib2.py
    >
    > def
    > __init__(self,rooturl,firsturl=[],forbidden=[],proxy={},cookie="",auth_basic=[]):
    > root=rooturl
    > self.excluded=forbidden
    > self.proxy=proxy
    > self.cookie=cookie
    > self.auth_basic=auth_basic
    > if root[-1]!="/":
    > root+="/"
    > if(self.checklink(root)):
    > print "Invalid link argument"
    > sys.exit(0)
    > for lien in firsturl:
    > if(self.checklink(lien)):


    More Pythonic for checklink to raise ValueError here than return
    non-zero.


    > print "Invalid link argument"
    > sys.exit(0)
    > server=(root.split("://")[1]).split("/")[0]

    [...]

    Better to use module urlparse.


    John
     
    John J. Lee, Jul 9, 2006
    #2
    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. m.banaouas

    urllib2 and HTTPBasicAuthHandler

    m.banaouas, Jan 16, 2007, in forum: Python
    Replies:
    3
    Views:
    576
    Max Erickson
    Jan 17, 2007
  2. MZ
    Replies:
    7
    Views:
    830
    Ed Mullen
    Mar 17, 2008
  3. Brendan
    Replies:
    4
    Views:
    311
    Brendan
    Aug 25, 2008
  4. Tilman
    Replies:
    0
    Views:
    417
    Tilman
    Mar 19, 2008
  5. rigo
    Replies:
    0
    Views:
    173
Loading...

Share This Page