HTTPBasicAuthHandler doesn't work

  • Thread starter nicolas.surribas
  • Start date
N

nicolas.surribas

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 !
 
J

John J. Lee

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top