Flask Api 在 Azure 应用服务 returns 中找不到 404
Flask Api In Azure App Service returns 404 Not Found
大家好,我希望你们都做得很好,
我的情况:
我在 Azure 应用程序服务中部署了一个烧瓶 api
将部署中心与 my git repo
一起使用
在本地测试代码时,一切似乎都按预期工作,但在 Azure 应用程序服务中
每个 url 与“url/something” returns :
Not Found The requested URL was not found on the server. If you
entered the URL manually please check your spelling and try again.
我的代码:
def convert(file):
with open(file, encoding='utf_8') as xml_file:
data_dict = xmltodict.parse(xml_file.read())
xml_file.close()
# generate the object using json.dumps()
# corresponding to json data
json_data = json.dumps(data_dict)
# Write the json data to output
# json file
with open("CvTest.json", "w", ) as json_file:
json_file.write(json_data)
json_file.close()
with open('CvTest.json') as f:
d = json.load(f)
df = pd.json_normalize(d)
df = df.to_dict('records')
df = pd.json_normalize(df)
data = df.to_json("file.json", orient="records")
with open('file.json', 'r') as data_file:
data = json.load(data_file)
for element in data:
element.pop('cv.@xmlns', None)
for element in data:
element.pop('cv.binaryDocuments.document', None)
with open('templates/data.json', 'w') as data_file:
data = json.dump(data, data_file)
with open('templates/data.json', 'r') as data_file:
data = json.load(data_file)
output = pd.DataFrame(data, columns=["cv.personalInformation.firstname",
"cv.personalInformation.lastname",
"cv.personalInformation.gender.code",
"cv.personalInformation.gender.name",
"cv.personalInformation.title",
"cv.personalInformation.isced.code",
"cv.personalInformation.isced.name",
"cv.personalInformation.birthyear",
"cv.personalInformation.civilState"
"cv.personalInformation.address.street",
"cv.personalInformation.address.postcode",
"cv.personalInformation.address.city",
"cv.personalInformation.address.country.code",
"cv.personalInformation.address.country.name",
"cv.personalInformation.address.state",
"cv.personalInformation.email",
"cv.personalInformation.phoneNumber",
"cv.personalInformation.homepage",
"cv.work.phase",
"cv.work.additionalText",
"cv.education.phase",
"cv.education.additionalText",
"cv.additionalInformation.language",
"cv.additionalInformation.competences",
"cv.additionalInformation", ])
output.to_csv('data.csv')
return jsonify(data)
UPLOAD_DIRECTORY = "templates"
if not os.path.exists(UPLOAD_DIRECTORY):
os.makedirs(UPLOAD_DIRECTORY)
api = Flask(__name__)
@api.route("/files")
def list_files():
"""Endpoint to list files on the server."""
files = []
for filename in os.listdir(UPLOAD_DIRECTORY):
path = os.path.join(UPLOAD_DIRECTORY, filename)
if os.path.isfile(path):
files.append(filename)
return jsonify(files)
@api.route("/files/<path:path>")
def get_file(path):
"""Download a file."""
return send_from_directory(UPLOAD_DIRECTORY, path, as_attachment=True)
@api.route("/files/<filename>", methods=["POST"])
def post_file(filename):
"""Upload a file."""
if "/" in filename:
# Return 400 BAD REQUEST
abort(400, "no subdirectories allowed")
with open(os.path.join(UPLOAD_DIRECTORY, filename), "wb") as fp:
fp.write(request.data)
return convert(os.path.join(UPLOAD_DIRECTORY,filename))
# Return 201 CREATED
return "", 201
if __name__ == "__main__":
api.run(debug=True)
要求:
Flask==2.0.2 xmltodict==0.12.0 pandas==1.3.4
这是我第一次在 azure 中使用应用服务
我希望有人能指导我正确的方向
谢谢
我不知道为什么,但是从 azure cli 而不是部署中心部署 webapp 对我有用。
大家好,我希望你们都做得很好,
我的情况:
我在 Azure 应用程序服务中部署了一个烧瓶 api
将部署中心与 my git repo
一起使用在本地测试代码时,一切似乎都按预期工作,但在 Azure 应用程序服务中 每个 url 与“url/something” returns :
Not Found The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.
我的代码:
def convert(file):
with open(file, encoding='utf_8') as xml_file:
data_dict = xmltodict.parse(xml_file.read())
xml_file.close()
# generate the object using json.dumps()
# corresponding to json data
json_data = json.dumps(data_dict)
# Write the json data to output
# json file
with open("CvTest.json", "w", ) as json_file:
json_file.write(json_data)
json_file.close()
with open('CvTest.json') as f:
d = json.load(f)
df = pd.json_normalize(d)
df = df.to_dict('records')
df = pd.json_normalize(df)
data = df.to_json("file.json", orient="records")
with open('file.json', 'r') as data_file:
data = json.load(data_file)
for element in data:
element.pop('cv.@xmlns', None)
for element in data:
element.pop('cv.binaryDocuments.document', None)
with open('templates/data.json', 'w') as data_file:
data = json.dump(data, data_file)
with open('templates/data.json', 'r') as data_file:
data = json.load(data_file)
output = pd.DataFrame(data, columns=["cv.personalInformation.firstname",
"cv.personalInformation.lastname",
"cv.personalInformation.gender.code",
"cv.personalInformation.gender.name",
"cv.personalInformation.title",
"cv.personalInformation.isced.code",
"cv.personalInformation.isced.name",
"cv.personalInformation.birthyear",
"cv.personalInformation.civilState"
"cv.personalInformation.address.street",
"cv.personalInformation.address.postcode",
"cv.personalInformation.address.city",
"cv.personalInformation.address.country.code",
"cv.personalInformation.address.country.name",
"cv.personalInformation.address.state",
"cv.personalInformation.email",
"cv.personalInformation.phoneNumber",
"cv.personalInformation.homepage",
"cv.work.phase",
"cv.work.additionalText",
"cv.education.phase",
"cv.education.additionalText",
"cv.additionalInformation.language",
"cv.additionalInformation.competences",
"cv.additionalInformation", ])
output.to_csv('data.csv')
return jsonify(data)
UPLOAD_DIRECTORY = "templates"
if not os.path.exists(UPLOAD_DIRECTORY):
os.makedirs(UPLOAD_DIRECTORY)
api = Flask(__name__)
@api.route("/files")
def list_files():
"""Endpoint to list files on the server."""
files = []
for filename in os.listdir(UPLOAD_DIRECTORY):
path = os.path.join(UPLOAD_DIRECTORY, filename)
if os.path.isfile(path):
files.append(filename)
return jsonify(files)
@api.route("/files/<path:path>")
def get_file(path):
"""Download a file."""
return send_from_directory(UPLOAD_DIRECTORY, path, as_attachment=True)
@api.route("/files/<filename>", methods=["POST"])
def post_file(filename):
"""Upload a file."""
if "/" in filename:
# Return 400 BAD REQUEST
abort(400, "no subdirectories allowed")
with open(os.path.join(UPLOAD_DIRECTORY, filename), "wb") as fp:
fp.write(request.data)
return convert(os.path.join(UPLOAD_DIRECTORY,filename))
# Return 201 CREATED
return "", 201
if __name__ == "__main__":
api.run(debug=True)
要求:
Flask==2.0.2 xmltodict==0.12.0 pandas==1.3.4
这是我第一次在 azure 中使用应用服务
我希望有人能指导我正确的方向
谢谢
我不知道为什么,但是从 azure cli 而不是部署中心部署 webapp 对我有用。