类型错误:尝试在烧瓶中发送Cookie时的预期字节数

时间:2019-07-07 15:13:22

标签: python flask brython

我正在尝试为我的项目创建一个登录网页,因为我使用了brython(浏览器python)作为前端。使用brython,我获得了表单数据,并将其发送到端点/ auth / login。有时没有收到数据,但是如果“记住我”复选框的值为true,则当我尝试返回cookie时会出现最大的问题,因此用户在访问网站时会自动登录,而不是使用浏览结束时会过期的会话

首先,我尝试发送两个响应cookie,一个用于记住用户,另一个用于存储用户的电子邮件,以便可以重新初始化session ['email']。我阅读了一些论坛,并意识到这是无法实现的,并且在flask文档中对此进行了确认。因此,我修改了代码并删除了电子邮件cookie,但是仍然遇到相同的错误。我还尝试将密钥更改为字节字符串,但仍然没有成功。抱歉,如果代码是意大利面条,但我还是Web开发的新手。如果需要其他任何帮助,请询问。

    #code in app.py
    @app.route('/auth/login', methods=['POST'])
    def user_authen():
        global login_success
        global login_tries
        email = request.form.get('email', 0, type=str)
        print(email)
        password = request.form.get('password', 0, type=str)
        print(password)
        remember = request.form.get('remember', 0, type=bool)
        print(remember)
        login_tries = request.form.get('tries', 0, type=int)
        print(login_tries)
        if User.login_valid(email, password):
            login_success = True
            User.login(email)
            if remember:
               resp = make_response("True")
               return resp.set_cookie('login', True, 
    expires=datetime.datetime.now() + datetime.timedelta(days=365))
           else:
                print(User.login_valid(email, password))
                return "True"
        else:
            print("login_valid", User.login_valid(email, password))
            session["email"] = None
            login_success = False
            return "return False"
Traceback (most recent call last):
  File "C:\Users\nayja\Desktop\Python Projects\.venvs\lpthw\lib\site-packages\flask\app.py", line 2328, in __call__
    return self.wsgi_app(environ, start_response)
  File "C:\Users\nayja\Desktop\Python Projects\.venvs\lpthw\lib\site-packages\flask\app.py", line 2314, in wsgi_app
    response = self.handle_exception(e)
  File "C:\Users\nayja\Desktop\Python Projects\.venvs\lpthw\lib\site-packages\flask\app.py", line 1760, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "C:\Users\nayja\Desktop\Python Projects\.venvs\lpthw\lib\site-packages\flask\_compat.py", line 36, in reraise
    raise value
  File "C:\Users\nayja\Desktop\Python Projects\.venvs\lpthw\lib\site-packages\flask\app.py", line 2311, in wsgi_app
    response = self.full_dispatch_request()
  File "C:\Users\nayja\Desktop\Python Projects\.venvs\lpthw\lib\site-packages\flask\app.py", line 1834, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "C:\Users\nayja\Desktop\Python Projects\.venvs\lpthw\lib\site-packages\flask\app.py", line 1737, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "C:\Users\nayja\Desktop\Python Projects\.venvs\lpthw\lib\site-packages\flask\_compat.py", line 36, in reraise
    raise value
  File "C:\Users\nayja\Desktop\Python Projects\.venvs\lpthw\lib\site-packages\flask\app.py", line 1832, in full_dispatch_request
    rv = self.dispatch_request()
  File "C:\Users\nayja\Desktop\Python Projects\.venvs\lpthw\lib\site-packages\flask\app.py", line 1818, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "C:\Users\nayja\Desktop\Python Projects\.venvs\projects\dynamic_web\src\app.py", line 145, in user_authen
    return resp.set_cookie('login', True, expires=datetime.datetime.now() + datetime.timedelta(days=365))
  File "C:\Users\nayja\Desktop\Python Projects\.venvs\lpthw\lib\site-packages\werkzeug\wrappers\base_response.py", line 481, in set_cookie
    samesite=samesite,
  File "C:\Users\nayja\Desktop\Python Projects\.venvs\lpthw\lib\site-packages\werkzeug\http.py", line 1144, in dump_cookie
    value = to_bytes(value, charset)
  File "C:\Users\nayja\Desktop\Python Projects\.venvs\lpthw\lib\site-packages\werkzeug\_compat.py", line 202, in to_bytes
    raise TypeError("Expected bytes")
TypeError: Expected bytes

0 个答案:

没有答案