I’m unable to write a custom urllib.FancyURLopener which initializes the user and passwd values in the constructor. I can get it to work fine with an empty constructor and a subsequent call to setpasswd. When I try to set the user and password in the constructor, I get strange errors regarding a missing attribute ‘tempcache’. Am I missing some Python syntax/usage regarding init?
I want to be able to specify my user and passwd in the constructor like so:
opener = MyRouterOpener("", "secretPassword")
page = opener.open("http://192.168.1.1/Status_Router.asp")
#do something with the page
Trying this way fails:
import urllib
class MyRouterOpener(urllib.FancyURLopener):
def __init__(self, user, passwd):
self.setpasswd(user, passwd)
def setpasswd(self, user, passwd):
self.__user = user
self.__passwd = passwd
def prompt_user_passwd(self, host, realm):
return self.__user, self.__passwd
opener = MyRouterOpener("", "secretPassword")
statusPage = opener.open("http://192.168.1.1/Status_Router.asp")
Output:
Traceback (most recent call last):
File "W:\Desktop\DynDns.enom.bad.py", line 15, in <module>
statusPage = opener.open("http://192.168.1.1/Status_Router.asp")
File "C:\Users\pengt\Python26\lib\urllib.py", line 179, in open
if self.tempcache and fullurl in self.tempcache:
AttributeError: RouterOpener instance has no attribute 'tempcache'
Exception AttributeError: "RouterOpener instance has no attribute 'tempcache'" in <bound method RouterOpener.__del__ of <__main__.RouterOpener instance at 0x022AB9E0>> ignored
I’ve also tried:
import urllib
class RouterOpener(urllib.FancyURLopener):
def __init__(self, user, passwd):
self.__user = user
self.__passwd = passwd
def prompt_user_passwd(self, host, realm):
return self.__user, self.__passwd
opener = RouterOpener("", "secretPassword")
statusPage = opener.open("http://192.168.1.1/Status_Router.asp")
But this produces:
Traceback (most recent call last):
File "W:\Desktop\DynDns.enom.bad.py", line 12, in <module>
statusPage = opener.open("http://192.168.1.1/Status_Router.asp")
File "C:\Users\pengt\Python26\lib\urllib.py", line 179, in open
if self.tempcache and fullurl in self.tempcache:
AttributeError: RouterOpener instance has no attribute 'tempcache'
Exception AttributeError: "RouterOpener instance has no attribute 'tempcache'" in <bound method RouterOpener.__del__ of <__main__.RouterOpener instance at 0x0231B968>> ignored
The only thing I’ve been able to get work is:
import urllib
class MyRouterOpener(urllib.FancyURLopener):
def setpasswd(self, user, passwd):
self.__user = user
self.__passwd = passwd
def prompt_user_passwd(self, host, realm):
return self.__user, self.__passwd
opener = MyRouterOpener()
opener.setpasswd("", "secretPassword")
statusPage = opener.open("http://192.168.1.1/Status_Router.asp")
But then I’m specifying my user and passwd on a separate line. I know this is no big deal really, but like I say above I’m really wanting to know if I’m missing something about Python regarding inheritance and init etc.
You probably have to call the parent object’s init which sets the tempcache attribute.
(Call it with the proper arguments for the urllib.FancyURLopener creator)