【问题标题】:Flask Api In Azure App Service returns 404 Not FoundAzure 应用服务中的 Flask Api 返回 404 Not Found
【发布时间】:2021-12-29 16:06:16
【问题描述】:

大家好,我希望你们都做得很好,

我的情况:

我在 azure 应用服务中部署了一个烧瓶 api

通过my git repo 使用部署中心

在本地everyting 中测试代码时似乎按预期工作,但在天蓝色应用程序服务中 每个带有“url/something”的网址都会返回:

未找到 在服务器上未找到请求的 URL。如果你 手动输入网址,请检查您的拼写并重试。

我的代码:

    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 门户中,重新启动 Web 应用并检查一次。
  • 而不是 '@api.route' ,将其更改为 '@app.route' 并检查一次
  • 好的,我试试改成@app.route
  • @C.Nivs im 使用网络应用程序 url apitest1712.azurewebsites.net

标签: python azure flask azure-web-app-service


【解决方案1】:

我不知道为什么,但是从 azure cli 而不是部署中心部署 webapp 对我有用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-12-09
    • 2015-12-04
    • 2020-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-03
    • 1970-01-01
    相关资源
    最近更新 更多