TypeError : encode() missing 1 required positional argument: 'iterations'

TypeError : encode() missing 1 required positional argument: 'iterations'

我不知道是什么触发了这个错误。我不知道为什么我一直收到这个错误。我已经更改了部分代码,但仍然出现此错误。我已经尝试修复它 2 天了。

回溯:

File "C:\Users\Adila\AppData\Local\Programs\Python\Python35\lib\site-packages\django\core\handlers\exception.py" in inner
  41.             response = get_response(request)

File "C:\Users\Adila\AppData\Local\Programs\Python\Python35\lib\site-packages\django\core\handlers\base.py" in _get_response
  187.                 response = self.process_exception_by_middleware(e, request)

File "C:\Users\Adila\AppData\Local\Programs\Python\Python35\lib\site-packages\django\core\handlers\base.py" in _get_response
  185.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "C:\Users\Adila\Documents\tryFOUR\src\register\views.py" in register
  13.           user = form.save()

File "C:\Users\Adila\Documents\tryFOUR\src\custom_user\forms.py" in save
  50.       user.set_password(self.cleaned_data["password2"])

File "C:\Users\Adila\AppData\Local\Programs\Python\Python35\lib\site-packages\django\contrib\auth\base_user.py" in set_password
  105.         self.password = make_password(raw_password)

File "C:\Users\Adila\AppData\Local\Programs\Python\Python35\lib\site-packages\django\contrib\auth\hashers.py" in make_password
  84.     return hasher.encode(password, salt)

Exception Type: TypeError at /accounts/register/
Exception Value: encode() missing 1 required positional argument: 'iterations'

hashers.py :

from django.contrib.auth.hashers import PBKDF2PasswordHasher
from django.utils.crypto import (get_random_string, pbkdf2)
from honeywordHasher.models import Sweetwords

class MyHoneywordHasher(PBKDF2PasswordHasher):
    algorithm = "honeyword_base9_tweak3_pbkdf2_sha256"
    iterations = PBKDF2PasswordHasher.iterations*3

    def hash(self, password, salt, iterations):
        hash = pbkdf2(password, salt, iterations, digest=self.digest)
        return base64.b64encode(hash).decode('ascii').strip()

    def salt(self):
        salt = get_random_string()
        while Sweetwords.objects.filter(salt=salt).exists():
            salt = get_random_string()

        return salt 

    def verify(self, password, encoded):
        algorithm, iterations, salt, dummy=encoded.split('$',3)
        hashes = pickle.loads(Sweetwords.objects.get(salt=salt).sweetwords)
        hash = self.hash(password, salt, int(iterations))
        if hash in hashes:
            return honeychecker.check_index(salt, hashes.index(hash))
        return False

    def encode(self, password, salt, iterations):
        sweetwords = ['hilman95']
        sweetwords.extend(honey_gen.gen(password, base64, ["passfiles.txt"]))
        for i in range(base64 + 1):
            sweetwords.extend(honeywordtweak.tweak(password[i], 3))
            random.shuffle(sweetwords)

        hashes = []
        for swd in sweetwords:
            hashes.append(self.hash(swd, salt, iterations))
        self.honeychecker.update_index(salt, sweetwords.index(password))
        h = Sweetwords(salt=salt, sweetwords = pickle.dumps(hashes))
        h.save()
        return "%s$%d$%s$%s" %(self.algorithm, iterations, salt, hashes[0])

有人能解释一下为什么我总是收到这个错误吗?我花了几个小时在这里搜索所有相关问题,但没有任何解决办法。

def encode(self, password, salt, iterations=None):
    if iterations is None:
        iterations = self.iterations
    # then ur code......

我在python中序列化和反序列化JSON的一个简单方法如下。 这可能不是最好的方法,但作为新手,在 python,它帮助我完成工作。

import json
import requests
class UserModel:
GET_USER_URL = "https://jsonplaceholder.typicode.com/todos/1"
#{'userId': 1, 'id': 1, 'title': 'delectus aut autem', 'completed': False}
def __init__(self) -> None:
    pass
def __init__(self, userId, id, title, completed):
    self.userId = userId
    self.id = id
    self.title = title
    self.completed = completed

def getUser():
    response = requests.get(UserModel.GET_USER_URL)
    res = response.json()
    print("Response\n",res)
    #dict_obj = json.loads(res)
    #print("Type of dict_obj", type(res))
    return json.dumps(res, indent=4, cls=UserEndoder)
# subclass
class UserEndoder(json.JSONEncoder):
    #I removed self as it was causing: 
    #TypeError: JSONEncoder.encode() missing 1 required positional argument: 'o' 
    def default(o):#I
        return o.__dict__

示例用法

userModel = UserModel.getUser()
print("Serialized results:\n",userModel)

userModel = UserModel(123, 321, "test title", True)
print("Deserialized:\n",UserEndoder.default(userModel))

祝你编码愉快!!!