收到 500 内部服务器错误 Flask 应用程序
Posted
技术标签:
【中文标题】收到 500 内部服务器错误 Flask 应用程序【英文标题】:getting 500 Internal server error Flask app 【发布时间】:2022-01-12 19:16:40 【问题描述】:尝试通过 Postman 访问 http://localhost:5000/token 时(POST 请求)
POST 请求详情: "电子邮件": "u999@jht.com", “密码”:“xdE56”
目的是测试登录功能。访问令牌应出现在 响应正文。 我收到以下错误
* Serving Flask app "app" (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: on
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!
* Debugger PIN: 112-744-944
127.0.0.1 - - [07/Dec/2021 18:08:40] "POST /token HTTP/1.1" 500 -
Traceback (most recent call last):
File "/root/PycharmProjects/pythonProject/smilecook-test-L_1-4/venv/lib/python3.7/site-packages/flask/app.py", line 2328, in __call__
return self.wsgi_app(environ, start_response)
File "/root/PycharmProjects/pythonProject/smilecook-test-L_1-4/venv/lib/python3.7/site-packages/flask/app.py", line 2314, in wsgi_app
response = self.handle_exception(e)
File "/root/PycharmProjects/pythonProject/smilecook-test-L_1-4/venv/lib/python3.7/site-packages/flask_restful/__init__.py", line 269, in error_router
return original_handler(e)
File "/root/PycharmProjects/pythonProject/smilecook-test-L_1-4/venv/lib/python3.7/site-packages/flask/app.py", line 1760, in handle_exception
reraise(exc_type, exc_value, tb)
File "/root/PycharmProjects/pythonProject/smilecook-test-L_1-4/venv/lib/python3.7/site-packages/flask/_compat.py", line 35, in reraise
raise value.with_traceback(tb)
File "/root/PycharmProjects/pythonProject/smilecook-test-L_1-4/venv/lib/python3.7/site-packages/flask/app.py", line 2311, in wsgi_app
response = self.full_dispatch_request()
File "/root/PycharmProjects/pythonProject/smilecook-test-L_1-4/venv/lib/python3.7/site-packages/flask/app.py", line 1834, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/root/PycharmProjects/pythonProject/smilecook-test-L_1-4/venv/lib/python3.7/site-packages/flask_restful/__init__.py", line 269, in error_router
return original_handler(e)
File "/root/PycharmProjects/pythonProject/smilecook-test-L_1-4/venv/lib/python3.7/site-packages/flask/app.py", line 1737, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/root/PycharmProjects/pythonProject/smilecook-test-L_1-4/venv/lib/python3.7/site-packages/flask/_compat.py", line 35, in reraise
raise value.with_traceback(tb)
File "/root/PycharmProjects/pythonProject/smilecook-test-L_1-4/venv/lib/python3.7/site-packages/flask/app.py", line 1832, in full_dispatch_request
rv = self.dispatch_request()
File "/root/PycharmProjects/pythonProject/smilecook-test-L_1-4/venv/lib/python3.7/site-packages/flask/app.py", line 1818, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/root/PycharmProjects/pythonProject/smilecook-test-L_1-4/venv/lib/python3.7/site-packages/flask_restful/__init__.py", line 458, in wrapper
resp = resource(*args, **kwargs)
File "/root/PycharmProjects/pythonProject/smilecook-test-L_1-4/venv/lib/python3.7/site-packages/flask/views.py", line 88, in view
return self.dispatch_request(*args, **kwargs)
File "/root/PycharmProjects/pythonProject/smilecook-test-L_1-4/venv/lib/python3.7/site-packages/flask_restful/__init__.py", line 573, in dispatch_request
resp = meth(*args, **kwargs)
File "/root/PycharmProjects/pythonProject/smilecook-test-L_1-4/resources/token_management.py", line 33, in post
access_token = create_access_token(identity=user.id, fresh=True)
File "/root/PycharmProjects/pythonProject/smilecook-test-L_1-4/venv/lib/python3.7/site-packages/flask_jwt_extended/utils.py", line 157, in create_access_token
return jwt_manager._create_access_token(identity, fresh, expires_delta, user_claims)
File "/root/PycharmProjects/pythonProject/smilecook-test-L_1-4/venv/lib/python3.7/site-packages/flask_jwt_extended/jwt_manager.py", line 479, in _create_access_token
json_encoder=config.json_encoder
File "/root/PycharmProjects/pythonProject/smilecook-test-L_1-4/venv/lib/python3.7/site-packages/flask_jwt_extended/tokens.py", line 77, in encode_access_token
json_encoder=json_encoder)
File "/root/PycharmProjects/pythonProject/smilecook-test-L_1-4/venv/lib/python3.7/site-packages/flask_jwt_extended/tokens.py", line 31, in _encode_jwt
json_encoder=json_encoder).decode('utf-8')
AttributeError: 'str' object has no attribute 'decode'
Postman 响应正文应显示 access_token。
编码问题有没有可能?
【问题讨论】:
您还没有发布有问题的 Python 代码。不过.decode()
可能是多余的。
你认为检查代码有用吗?我要发吗?
以防万一...这里有***.com/questions/70258682/…我问了另一个实际上与同一问题相关的问题..谢谢。
【参考方案1】:
在 Flask 中包含处理路由的一段代码(即在 POST 之后进行处理)通常可以更容易地调试问题。
从错误日志中,我怀疑某处有一段代码正在执行<string>.decode('utf-8')
,但在 Python3 中,所有字符串都是 unicode,因此通常的解决方案是删除 decode('utf-8')
。如果这不是您的代码(例如您使用的是库),您需要确保您使用的库的版本与 Python3 兼容,例如如果这是您正在使用的库,请参阅 PyJWT
库的 this *** 响应。
【讨论】:
.decode('utf-8') 不是我的代码的一部分。 Flask-JWT-Extended 3.20.0 也是我用作库的工具以上是关于收到 500 内部服务器错误 Flask 应用程序的主要内容,如果未能解决你的问题,请参考以下文章
如何使用弹性 beantalk 和 aws 修复 Flask 应用程序中的内部服务器错误
使用烧瓶询问的 Alexa 技能:HTTP 500:内部服务器错误
来自 Javascript AJAX 的 ASP.NET MVC 操作调用收到 500 内部服务器错误