如何通过flask从python脚本返回准确的错误



我正在调用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 

我是通过邮局来打电话的,退货声明正在起作用。但我正在从返回中做出响应,并传递resp.status代码,而该部分正在失败。

后人工错误

<!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)) 

这已经解决了。

相关内容

  • 没有找到相关文章

最新更新