gpt4 book ai didi

使用 Openshift 时出现 Python Rest Http 错误 (Flask)

转载 作者:太空宇宙 更新时间:2023-11-03 16:34:49 25 4
gpt4 key购买 nike

我有一些简单的代码可以在 Python 中引发错误:

class LoginUser(Resource):
def post(self):
# Parse the arguments
parser = reqparse.RequestParser()
parser.add_argument('email', type=str, help='Email address to create user')
args = parser.parse_args()

_userEmail = args['email']
_users = User.objects(email=_userEmail)

if len(_users) == 0:
raise LoginInvalidEmailError()

还有一些类(class)

from flask_restful import HTTPException

class LoginInvalidEmailError(HTTPException):
pass

class LoginInvalidPasswordError(HTTPException):
pass

#http://flask-restful.readthedocs.io/en/latest/extending.html#define-custom-error-messages
custom_errors = {
'LoginInvalidEmailError': {
'message': "Email address has not been registered.",
"code" : 500,
"status": 500,
"status_code": 500
},
'LoginInvalidPasswordError': {
'message': "Invalid password.",
"code" : 500,
"status": 500,
"status_code": 500
}
}

在我的本地环境中,它返回 500 错误,如下所示:

{
"data": {
"code": 500,
"message": "Email address has not been registered.",
"status": 500,
"status_code": 500
}, "status": 500, "config": {
"method": "POST",
"transformRequest": [null],
"transformResponse": [null],
"url": "http://localhost:3030/User/Login",
"data": {
"email": "",
"password": ""
},
"headers": {
"Accept": "application/json, text/plain, */*",
"Content-Type": "application/json;charset=utf-8",
"Authorization": "token null",
"X-XSRF-TOKEN": "62PQ0ri9DC/ZStMLUkR9st7UvOtGVRLs88zoE="
}
}, "statusText": "INTERNAL SERVER ERROR"
}

但是在 Openshift 中它提示:

Traceback (most recent call last):
File "/var/lib/openshift/571e777e89f5cfb65e00012b/app-root/runtime/dependencies/python/virtenv/lib/python2.7/site-py2.7.egg/flask/app.py", line 1836, in __call__
return self.wsgi_app(environ, start_response)
File "/var/lib/openshift/571e777e89f5cfb65e00012b/app-root/runtime/dependencies/python/virtenv/lib/python2.7/site-py2.7.egg/flask/app.py", line 1820, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/var/lib/openshift/571e777e89f5cfb65e00012b/app-root/runtime/dependencies/python/virtenv/lib/python2.7/site-p__init__.py", line 271, in error_router
return original_handler(e)
File "/var/lib/openshift/571e777e89f5cfb65e00012b/app-root/runtime/dependencies/python/virtenv/lib/python2.7/site-py2.7.egg/flask/app.py", line 1403, in handle_exception
reraise(exc_type, exc_value, tb)
File "/var/lib/openshift/571e777e89f5cfb65e00012b/app-root/runtime/dependencies/python/virtenv/lib/python2.7/site-p__init__.py", line 268, in error_router
return self.handle_error(e)
File "/var/lib/openshift/571e777e89f5cfb65e00012b/app-root/runtime/dependencies/python/virtenv/lib/python2.7/site-py2.7.egg/flask/app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "/var/lib/openshift/571e777e89f5cfb65e00012b/app-root/runtime/dependencies/python/virtenv/lib/python2.7/site-py2.7.egg/flask/app.py", line 1477, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/var/lib/openshift/571e777e89f5cfb65e00012b/app-root/runtime/dependencies/python/virtenv/lib/python2.7/site-p__init__.py", line 271, in error_router
return original_handler(e)
File "/var/lib/openshift/571e777e89f5cfb65e00012b/app-root/runtime/dependencies/python/virtenv/lib/python2.7/site-py2.7.egg/flask/app.py", line 1381, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/var/lib/openshift/571e777e89f5cfb65e00012b/app-root/runtime/dependencies/python/virtenv/lib/python2.7/site-p__init__.py", line 268, in error_router
return self.handle_error(e)
File "/var/lib/openshift/571e777e89f5cfb65e00012b/app-root/runtime/dependencies/python/virtenv/lib/python2.7/site-py2.7.egg/flask/app.py", line 1475, in full_dispatch_request
rv = self.dispatch_request()
File "/var/lib/openshift/571e777e89f5cfb65e00012b/app-root/runtime/dependencies/python/virtenv/lib/python2.7/site-py2.7.egg/flask/app.py", line 1461, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/var/lib/openshift/571e777e89f5cfb65e00012b/app-root/runtime/dependencies/python/virtenv/lib/python2.7/site-p__init__.py", line 477, in wrapper
resp = resource(*args, **kwargs)
File "/var/lib/openshift/571e777e89f5cfb65e00012b/app-root/runtime/dependencies/python/virtenv/lib/python2.7/site-py2.7.egg/flask/views.py", line 84, in view
return self.dispatch_request(*args, **kwargs)
File "/var/lib/openshift/571e777e89f5cfb65e00012b/app-root/runtime/dependencies/python/virtenv/lib/python2.7/site-p__init__.py", line 587, in dispatch_request
resp = meth(*args, **kwargs)
File "/var/lib/openshift/571e777e89f5cfb65e00012b/app-root/runtime/repo/kb/endpoints/users/LoginUser.py", line 19,
raise LoginInvalidEmailError()
File "/opt/rh/python27/root/usr/lib/python2.7/site-packages/werkzeug/exceptions.py", line 75, in __init__
Exception.__init__(self, '%d %s' % (self.code, self.name))
File "/opt/rh/python27/root/usr/lib/python2.7/site-packages/werkzeug/exceptions.py", line 95, in name
return HTTP_STATUS_CODES[self.code]
KeyError: None

有人经历过这种情况或者知道为什么环境之间存在差异吗?

最佳答案

尝试在 github 存储库上的 werkzeug/exceptions.py:95 上跟踪错误时 [1] ,你会发现导致 LOC 实际上不是第 95 行,而是第 109 行 [2]

File "/opt/rh/python27/root/usr/lib/python2.7/site-packages/werkzeug/exceptions.py", line 95, in name
return HTTP_STATUS_CODES[self.code]

应该是:

@property
def name(self):
"""The status name."""
return HTTP_STATUS_CODES.get(self.code, 'Unknown Error')

逻辑结论是 Openshift 上安装的版本和本地计算机上安装的版本不同,因为您可能没有在 requirements.txt 上明确定义 Werkzeug。

我通过创建一个应用程序并在我的计算机上复制了此问题:

$ pip freeze
Werkzeug==0.11.4

在 Openshift 上:

$ pip freeze
Werkzeug==0.8.3

因此,只需添加 Werkzeug==0.11.4 就可以开始了。

关于使用 Openshift 时出现 Python Rest Http 错误 (Flask),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37311813/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com