为什么我不能__init__我的自定义FancyURLopener与用户& passwd文件

时间:2011-05-18 03:42:12

标签: python urllib

我无法编写自定义urllib.FancyURLopener,它会在构造函数中初始化user和passwd值。我可以使用空构造函数和后续调用setpasswd来使其正常工作。当我尝试在构造函数中设置用户和密码时,我会发现有关缺少属性“tempcache”的奇怪错误。我是否遗漏了关于 init 的一些Python语法/用法?

我希望能够在构造函数中指定我的用户和passwd,如下所示:

opener = MyRouterOpener("", "secretPassword")
page = opener.open("http://192.168.1.1/Status_Router.asp")
#do something with the page

尝试这种方式失败:

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")

输出:

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

我也试过了:

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")

但这会产生:

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

我唯一能做到的就是:

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")

然后我在另一条线上指定我的用户和passwd。我知道这真的没什么大不了的,但就像我上面说的那样,我真的想知道我是否遗漏了关于继承和 init 等的Python。

2 个答案:

答案 0 :(得分:5)

您可能必须调用父对象的 init 来设置tempcache属性。

def __init__(self, user, passwd):
    self.__user = user
    self.__passwd = passwd
    urllib.FancyURLopener.__init__(self)

(使用urllib.FancyURLopener创建者的正确参数调用它)

答案 1 :(得分:0)

确保调用超类“__init__方法,如下所示:

class MyRouterOpener(urllib.FancyURLopener):
    def __init__(self, user, passwd):
        # grr, old-style classes. :(
        urllib.FancyURLopener.__init__(self)

        # fancy custom fun stuff!
        self.__user = user
        self.__passwd = passwd