我正在使用python / flask为我的网站构建一个简单的登录页面,并且无法验证从数据库中提取的哈希值。提交表单时,出现内部服务器错误。 apache日志显示“类型为“ TypeError'的对象不是JSON可序列化的对象。”我创建了多个用户,因此它似乎与单个哈希无关。感谢您的帮助
app.route("/login/",methods=['GET','POST'])
def loginpage():
error=''
try:
c,conn= connectorConnection()
if request.method == 'POST':
c.execute("select * from users where username = ('%s')" %
request.form['username'])
data = c.fetchone()
if sha256_crypt.verify(request.form['password'], data):
session['logged_in'] = True
session['username'] = request.form['username']
flash(' you are now logged in')
return redirect(url_for('home'))
else:
error = 'Invalid Credentials'
gc.collect()
return render_template('login.html',error=error)
Stracktrace:
Traceback (most recent call last):
File "C:\Users\Tank\AppData\Local\Programs\Python\Python36\lib\site-packages\flask\app.py", line 2292, in wsgi_app
response = self.full_dispatch_request()
File "C:\Users\Tank\AppData\Local\Programs\Python\Python36\lib\site-packages\flask\app.py", line 1816, in full_dispatch_request
return self.finalize_request(rv)
File "C:\Users\Tank\AppData\Local\Programs\Python\Python36\lib\site-packages\flask\app.py", line 1833, in finalize_request
response = self.process_response(response)
File "C:\Users\Tank\AppData\Local\Programs\Python\Python36\lib\site-packages\flask\app.py", line 2114, in process_response
self.session_interface.save_session(self, ctx.session, response)
File "C:\Users\Tank\AppData\Local\Programs\Python\Python36\lib\site-packages\flask\sessions.py", line 375, in save_session
val = self.get_signing_serializer(app).dumps(dict(session))
File "C:\Users\Tank\AppData\Local\Programs\Python\Python36\lib\site-packages\itsdangerous\serializer.py", line 166, in dumps
payload = want_bytes(self.dump_payload(obj))
File "C:\Users\Tank\AppData\Local\Programs\Python\Python36\lib\site-packages\itsdangerous\url_safe.py", line 42, in dump_payload
json = super(URLSafeSerializerMixin, self).dump_payload(obj)
File "C:\Users\Tank\AppData\Local\Programs\Python\Python36\lib\site-packages\itsdangerous\serializer.py", line 133, in dump_payload
return want_bytes(self.serializer.dumps(obj, **self.serializer_kwargs))
File "C:\Users\Tank\AppData\Local\Programs\Python\Python36\lib\site-packages\flask\json\tag.py", line 296, in dumps
return dumps(self.tag(value), separators=(',', ':'))
File "C:\Users\Tank\AppData\Local\Programs\Python\Python36\lib\site-packages\flask\json\__init__.py", line 179, in dumps
rv = _json.dumps(obj, **kwargs)
File "C:\Users\Tank\AppData\Local\Programs\Python\Python36\lib\json\__init__.py", line 238, in dumps
**kw).encode(obj)
File "C:\Users\Tank\AppData\Local\Programs\Python\Python36\lib\json\encoder.py", line 199, in encode
chunks = self.iterencode(o, _one_shot=True)
File "C:\Users\Tank\AppData\Local\Programs\Python\Python36\lib\json\encoder.py", line 257, in iterencode
return _iterencode(o, 0)
File "C:\Users\Tank\AppData\Local\Programs\Python\Python36\lib\site-packages\flask\json\__init__.py", line 81, in default
return _json.JSONEncoder.default(self, o)
File "C:\Users\Tank\AppData\Local\Programs\Python\Python36\lib\json\encoder.py", line 180, in default
o.__class__.__name__)
TypeError: Object of type 'TypeError' is not JSON serializable
127.0.0.1 - - [18/Feb/2019 20:23:40] "[1m[35mPOST /login/ HTTP/1.1[0m" 500 -
答案 0 :(得分:1)
app.route("/login/",methods=['GET','POST'])
def loginpage():
try:
your code here
except Exception as e:
print(e)
# Now you can see what the real issue is...
return json.dumps({'success':True}), 200,
{'ContentType':'application/json'})
答案 1 :(得分:0)
根据balderman的建议添加try / except后,错误显示为“哈希必须是unicode或字节,而不是元组”。原来我的数据库中的密码表太短了(45个字符)。更改表以允许200个字符允许插入完整的哈希。感谢您的帮助