如何通过 Flask 从 python 脚本中 return 确切错误
How to return exact error from the python script through flask
我正在调用 python 函数来调用脚本来创建目录。
app = Flask(__name__)
@app.route('/', methods=['POST'])
def share_drive():
try:
parentdir = request.json.get("parentdir")
dirname = request.json.get("dirname")
#parentdir = request.values.get("parentdir")
#dirname = request.values.get("dirname")
path = os.path.join(parentdir, dirname)
# makedirs create directory recursively
try:
os.makedirs(path)
#return ("Success Fileshare created: {} ".format(dirname))
resp = make_resopnse('{} successfully created.'.format(dirname))
resp.status_code = 200
return resp
except OSError as error:
#return ("Fileshare creation failed: {} ".format(dirname))
resp = make_resopnse('Failed to create fileshare {}'.format(dirname))
resp.status_code = 400
return resp
我正在通过 post man 调用它,return 语句正在运行。但是我正在从 return 做出回应并传递 resp.status 代码,那部分失败了。
post man
出错
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>500 Internal Server Error</title>
<h1>Internal Server Error</h1>
<p>The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.</p>
因为你的except
有错别字:
return ("Fileshare creation failed: {} ". formart(dirname))
应该是:
return ("Fileshare creation failed: {} ".format(dirname))
并且,如果 dirname
未定义(它应该始终是,但显示确切错误的良好编码形式):
print(error)
return ("Fileshare creation failed: {} ".format(dirname))
os.makedirs(path)
#return ("Success Fileshare created: {} ".format(dirname))
resp = Response('{} successfully created.'.format(dirname))
print (resp)
resp.status_code = 200
return resp
except OSError as error:
#print(error)
resp = Response('{} fileshare creation failed.{} filename already exists'.format(dirname, error))
print (resp)
resp.status_code = 200
return resp
#return ("Fileshare creation failed: {} ".format(dirname))
这已修复。
我正在调用 python 函数来调用脚本来创建目录。
app = Flask(__name__)
@app.route('/', methods=['POST'])
def share_drive():
try:
parentdir = request.json.get("parentdir")
dirname = request.json.get("dirname")
#parentdir = request.values.get("parentdir")
#dirname = request.values.get("dirname")
path = os.path.join(parentdir, dirname)
# makedirs create directory recursively
try:
os.makedirs(path)
#return ("Success Fileshare created: {} ".format(dirname))
resp = make_resopnse('{} successfully created.'.format(dirname))
resp.status_code = 200
return resp
except OSError as error:
#return ("Fileshare creation failed: {} ".format(dirname))
resp = make_resopnse('Failed to create fileshare {}'.format(dirname))
resp.status_code = 400
return resp
我正在通过 post man 调用它,return 语句正在运行。但是我正在从 return 做出回应并传递 resp.status 代码,那部分失败了。
post man
出错<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>500 Internal Server Error</title>
<h1>Internal Server Error</h1>
<p>The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.</p>
因为你的except
有错别字:
return ("Fileshare creation failed: {} ". formart(dirname))
应该是:
return ("Fileshare creation failed: {} ".format(dirname))
并且,如果 dirname
未定义(它应该始终是,但显示确切错误的良好编码形式):
print(error)
return ("Fileshare creation failed: {} ".format(dirname))
os.makedirs(path)
#return ("Success Fileshare created: {} ".format(dirname))
resp = Response('{} successfully created.'.format(dirname))
print (resp)
resp.status_code = 200
return resp
except OSError as error:
#print(error)
resp = Response('{} fileshare creation failed.{} filename already exists'.format(dirname, error))
print (resp)
resp.status_code = 200
return resp
#return ("Fileshare creation failed: {} ".format(dirname))
这已修复。