itsdangerous 产生token



import itsdangerous #产生token



def create_token(data,expires_time=3600):
    salt = '!@#'
    t = itsdangerous.TimedJSONWebSignatureSerializer(salt,expires_in=expires_time)  # 产生一个token
    res = t.dumps(data)
    token = res.decode()
    print("产生的token:\n",token)
    return token

def check_token(token):
    t = itsdangerous.TimedJSONWebSignatureSerializer("!@#")  # 产生一个token
    try:
        res = t.loads(token)
    except Exception as e:
        print(e)
        print("出错了,token失效")
    else:
        print("解析的token:\n",res)
        return res
    finally:
        print("finally---")

data=create_token({"username": "niuhanyang"})
check_token(data)

# 异常处理:

 


上一篇:itsdangerous生成带有效期的token


下一篇:Django发送邮件和itsdangerous模块的配合使用