我正在尝试创建一个扩展HTTPBasicAuthHandler的类。出于某种原因,我在旧代码中使用的相同方法在这里不起作用。
class AuthInfo(urllib2.HTTPBasicAuthHandler):
def __init__(self, realm, url, username, password):
self.pwdmgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
self.pwdmgr.add_password(None, url, username, password)
super(AuthInfo, self).__init__(self.pwdmgr)
这是错误:
Traceback (most recent call last):
File "./RestResult.py", line 67, in ?
auth = AuthInfo(None, "default", "xxxxx", "xxxxxxxx")
File "./RestResult.py", line 47, in __init__
super(AuthInfo, self).__init__(self.pwdmgr)
TypeError: super() argument 1 must be type, not classobj
答案 0 :(得分:7)
urllib2.HTTPBasicAuthHandler
类是旧式类(它不会从object
继承),这意味着它不能与super
一起使用。您必须直接调用__init__
:
urllib2.HTTPBasicAuthHandler.__init__(self, self.pwdmgr)