0

I thought I was calling this correctly but obviously not. Can someone tell me what I am doing wrong?

import urllib.request

class testURLopener(urllib.request.FancyURLopener):

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


if __name__ == '__main__' :                

   opener = testURLopener()

   opener.setpasswd("user", "password")

   web_byte = opener.open('https://www.test.com')

   print(web_byte.getcode() )
theakson
  • 502
  • 7
  • 23
  • What's the error output? You've subclassed `urllib.request` FancyURLopener and trying to kind of call super of `urllib` FancyURLopener's `__init__`. – ipaleka Sep 30 '19 at 11:26
  • here's what happens if you run the code TypeError: __init__() missing 2 required positional arguments: 'user' and 'passwd' – theakson Sep 30 '19 at 11:29

1 Answers1

2

Here is the problem:

opener = testURLopener()

as you set a condition ion your init to get the username and password. You may change the arguments to named instead of positional and set the default values with the following code (you would need the correct namespace for FancyURLopener):

def __init__(self, user="", passwd=""):
    self.__user = user
    self.__passwd = passwd
    urllib.request.FancyURLopener.__init__(self)
ipaleka
  • 3,745
  • 2
  • 13
  • 33
  • I changed to super().__init__(self, user, passwd) and the call to opener = AppURLopener( user = "testuser" , passwd = "test_pass" ) and now I'm getting TypeError: __init__() got an unexpected keyword argument 'user' I am losing it I know but I just can't see what I am doing wrong? – theakson Sep 30 '19 at 12:29
  • 1
    If you use `super()` then don't pass self, since you're already calling the method on self. Also the parent class `FancyURLopener` does **not** take `user` and `password` as parameters in its initialiser. Therefore, `super().__init__()` should be called. But the above answer should also work, since `__init__` is called on the parent class you need to pass `self`. – dirkgroten Sep 30 '19 at 12:37
  • thanks both. I REALLY concentrated on what you both said and it now gets me error 403 which is another adventure. Thanks again for setting my mind straight. – theakson Sep 30 '19 at 13:08