admin管理员组

文章数量:1313794

I'm trying to get my model deployed on iis, but after I browse the application, it throws Not Found The requested URL was not found on the server. I follow this with web.config and use wfastcgi module. At first it was working yet with problem that only server_ip and localhost is accessible, not other client_ip in the same network. When I browse the web keep loading pending documents. So, I did remove application from website in iis and iisreset

At the moment, it seems like Flask throws this error at first and I have no clue what could go wrong? I want to deploy Flask as API endpoint.

My code snippet:

app = Flask(__name__)

model = load_model('./saved_model.keras')

@app.route("/api/index", methods=['GET'])
def index():
    return 'Welcome!'

@app.route("/api/predict", methods=['POST'])
def predict():
    data = request.get_json()

    // Decode and normalize image

    print(f'Predicted class: {predicted_class}')
    return jsonify({'prediction': str(predicted_class)}), 200

if __name__ == '__main__':
    app.run(debug=True)

I add application under Default Web Site in iis that binds to port 80

I'm trying to get my model deployed on iis, but after I browse the application, it throws Not Found The requested URL was not found on the server. I follow this with web.config https://learn.microsoft/it-it/visualstudio/python/configure-web-apps-for-iis-windows?view=vs-2022 and use wfastcgi module. At first it was working yet with problem that only server_ip and localhost is accessible, not other client_ip in the same network. When I browse the web keep loading pending documents. So, I did remove application from website in iis and iisreset

At the moment, it seems like Flask throws this error at first and I have no clue what could go wrong? I want to deploy Flask as API endpoint.

My code snippet:

app = Flask(__name__)

model = load_model('./saved_model.keras')

@app.route("/api/index", methods=['GET'])
def index():
    return 'Welcome!'

@app.route("/api/predict", methods=['POST'])
def predict():
    data = request.get_json()

    // Decode and normalize image

    print(f'Predicted class: {predicted_class}')
    return jsonify({'prediction': str(predicted_class)}), 200

if __name__ == '__main__':
    app.run(debug=True)

I add application under Default Web Site in iis that binds to port 80

Share Improve this question asked Jan 31 at 4:07 CChickiiCChickii 95 bronze badges 5
  • We can Run Flask Web Apps on IIS with HttpPlatformHandler. – Jason Pan Commented Jan 31 at 7:23
  • We can find the warinning: the WFastCGI project is no longer maintained and may result in bugs. So please use httpplatformhandler. – Jason Pan Commented Jan 31 at 7:29
  • Thanks! In any chance, do you know how can I use virtual environment in processPath in <httpPlatform />? I have libraries and packages in that. – CChickii Commented Jan 31 at 8:41
  • Don’t use virtual environments with IIS/Windows. Use containers to isolate if needed. – Lex Li Commented Jan 31 at 17:11
  • Ok, after I used base environment there's python log showing it was running but still 404 in page load – CChickii Commented Feb 4 at 9:47
Add a comment  | 

1 Answer 1

Reset to default 0

You could follow the below steps to host the flask api on iis and access on the remote machine.

i have this below app.py file:

from flask import Flask, request, jsonify

app = Flask(__name__, static_url_path='/flaskapp')

@app.route("/flaskapp/api/index", methods=['GET'])
def index():
    return jsonify({"message": "Welcome to the Flask API!"})

@app.route("/flaskapp/api/predict", methods=['POST'])
def predict():
    try:
        data = request.get_json()

        # Check if required field is present
        if "input_data" not in data:
            return jsonify({"error": "Missing 'input_data' key in request"}), 400

        input_value = data["input_data"]

        # Dummy processing: Just return the received value as "predicted_class"
        predicted_class = f"Processed: {input_value}"

        print(f'Predicted class: {predicted_class}')
        return jsonify({'prediction': predicted_class}), 200

    except Exception as e:  # Move this to the correct indentation level
        return jsonify({"error": str(e)}), 500  # Return error response

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000, debug=True)

use application name which is flaskapp in app = Flask(__name__, static_url_path='/flaskapp') if you are hosting as application in iis.

  • Create new application in iis with the folder path where you have app.py file. Make sure you have created new app pool for this application with below configuration.

  • Go to the iis and select your application from the left pane.
  • Double click Handler mapping

  • Click Add module mapping from Action pane

  • Set the values as shown below:

In the executable add path:

C:\Python\python.exe|C:\Python\Lib\site-packages\wfastcgi.py

Request path: *

Module: FastCgiModule

Name: Your choice

  • Now go back and select server node where you can see the FastCGISetting

  • Select your paython application setting.

  • There you can see the Environment variable. Add these name value pair.

WSGI_HANDLER: app.app

PYTHONPATH: your application folder full path

  • Set the iis site bindings with the http and port 5000 or any of your choice

wfastcgi.py:

from app import app
if __name__ == "__main__":
    app.run()

web.config:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <handlers>
            <add name="Python FastCGI" path="*" verb="*" modules="FastCgiModule" scriptProcessor="C:\Python\python.exe|C:\Python\Lib\site-packages\wfastcgi.py" resourceType="Unspecified" requireAccess="Script" />
        </handlers>
    </system.webServer>
</configuration>
  • Set iis_iusrs and iusr permission to the site root folder and python root folder.
  • Add inbound rule in firewall with the port number 5000 so that remote machine can access this site.

Result:

本文标签: pythonFlask Not Found The requested URL was not found on the serverStack Overflow