Search code examples
pythonfancyurlopener

can't send user and password to FancyURLopener getting missing 2 required positional arguments: 'user' and 'passwd'


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

Solution

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