Note that there are some explanatory texts on larger screens.

plurals
  1. POwhy can't i __init__ my custom FancyURLopener with the user & passwd
    text
    copied!<p>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 <strong>init</strong>?</p> <p>I want to be able to specify my user and passwd in the constructor like so:</p> <pre><code>opener = MyRouterOpener("", "secretPassword") page = opener.open("http://192.168.1.1/Status_Router.asp") #do something with the page </code></pre> <p>Trying this way fails:</p> <pre><code>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") </code></pre> <p>Output:</p> <pre><code>Traceback (most recent call last): File "W:\Desktop\DynDns.enom.bad.py", line 15, in &lt;module&gt; 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 &lt;bound method RouterOpener.__del__ of &lt;__main__.RouterOpener instance at 0x022AB9E0&gt;&gt; ignored </code></pre> <p>I've also tried:</p> <pre><code>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") </code></pre> <p>But this produces:</p> <pre><code>Traceback (most recent call last): File "W:\Desktop\DynDns.enom.bad.py", line 12, in &lt;module&gt; 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 &lt;bound method RouterOpener.__del__ of &lt;__main__.RouterOpener instance at 0x0231B968&gt;&gt; ignored </code></pre> <p>The only thing I've been able to get work is:</p> <pre><code>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") </code></pre> <p>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 <strong>init</strong> etc.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload