HTTPBasicAuthHandler doesn't work

nicolas.surribas at gmail.com nicolas.surribas at gmail.com
Sun Jul 9 09:33:40 EDT 2006


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 !




More information about the Python-list mailing list