# $Id: URLOpener.py,v 1.6 2002/05/02 08:24:48 jmp Exp $ # # Copyright 2001, 2002 by Fle3 Team and contributors # # This file is part of Fle3. # # Fle3 is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # Fle3 is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with Fle3; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA __version__ = "$Revision: 1.6 $"[11:-2] import urllib, os class URLOpener(urllib.FancyURLopener): def __init__(self, uname, pwd, *args): apply(urllib.FancyURLopener.__init__, (self,)+args) self.uname = uname self.pwd = pwd self.prompted = 0 def prompt_user_password(self, host, realm): """Want's password, and stuff.""" if self.prompted: raise 'Unauthorized', 'Username and password unauthorized.' else: self.prompted = 1 return self.uname, self.pwd def open_url(url, uname='', passwd=''): """Open url (with uname and password, if required).""" urllib._urlopener = URLOpener(uname, passwd) file, headers = urllib.urlretrieve("%s" % (url)) os.unlink(file) return headers # EOF