TransWikia.com

bind python/flask module app yo WINDOWS Server IIS

Stack Overflow Asked by Shaul.Sondak on January 1, 2022

I’m a novice python developer with very basic knowledge of this platform. We received a python app developed on Mac/Linux from a 3rd parry (consultant company), and I’m having trouble bind it to IIS on a WINDOWS WEB server.

Setting:

  • Python 3.8.1, deploying flask and dash
  • Windows server 2012R2
  • IIS 8.5

Configuration:

  • FastCgi setting on IIS :
    enter image description here

  • Create a site under IIS (port 8051 in my test) and set the physical path to the folder where the runserver.py and web.config files reside.
    enter image description here

enter image description here

  • The rest of the configuration is administrated by a web.config file at the app’s root folder.

Successful Simple Test

  • I successfully implemented a simple test file as follow:

runserver.py

                from flask import Flask
            app = Flask(__name__)

            from datetime import datetime 
            from urllib.parse import urlparse
            from flask import request
             
            import socket

            app.debug = True

            @app.route('/')
            def kuki():    
                return "Hello from flask over IIS. host:[{}],  url:[{}],  TS:[{}]".format(socket.gethostname(), request.base_url, datetime.now())

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

web.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <system.webServer>
        <handlers>
            <remove name="PythonHandler" />
            <add    
                    name="PythonHandler" 
                    path="*" 
                    verb="*" 
                    modules="FastCgiModule" 
                    scriptProcessor="c:python3_8_1python.exe|c:python3_8_1Libsite-packageswfastcgi.py" 
                    resourceType="Unspecified" 
                    requireAccess="Script" 
            />
        </handlers>
    </system.webServer>
    <appSettings>
        <add key="PYTHONPATH" value="D:_MyPythonHelloFlask" />
        <add key="WSGI_HANDLER" value="runserver.app" />
        <add key="WSGI_LOG" value="D:_MyPythonHelloFlasklogHelloFlask.log" />
    </appSettings>  
</configuration>

Problem running the full python app

  • An application folder reside under the app’s root folder
    enter image description here
    enter image description here

  • The py -m application command is successfully launching the service

  • Can someone advice how to set the web.config file to enable successful IIS binding?

Thanks,

Shaul

One Answer

index.py:

# Import Application level packages
import dash_html_components as html
import dash_bootstrap_components as dbc

# Import other neccessary classes, objects in project
from app import dash_app
from xxx import XXX
...
...

# Import Application dependent packages
import random
from datetime import date
random.seed(date.today())

# Set the app's HTML title
dash_app.title = "**** *****"

# Set the app's overall layout. 
# Each Content Element is imported by reference and 
# elements in each container will change dynamically (when respective callback is fired) as user enters inputs.
dash_app.layout = html.Div(
    ....
    ....
    ....
)

ContentElement.init_all_callbacks(skip=[])

# Start flask server
flask_app = dash_app.server

# Run application
def run():

    # open webbrowser automatically:
    if Config.AUTOOPEN:
        import threading, webbrowser
        url = f"http://{Config.HOST}:{Config.PORT}"
        # Wait for 2.25 seconds initially for dash to process the app initially
        threading.Timer(2.25, lambda: webbrowser.open(url)).start()

    # Add debug=Config.DEBUG as a parameter in order to debug in front-end
    dash_app.run_server(host=Config.HOST, port=Config.PORT)

app.py:

# Import OS level packages
import os

# Import Application level packages
from flask import send_from_directory
import dash
import dash_bootstrap_components as dbc

# Pull in bootstrap darkly stylesheet
external_stylesheets = [
    r"assets/bootstrap_style.css"
]

# Create instance of dash app
dash_app = dash.Dash(__name__, external_stylesheets=external_stylesheets, url_base_pathname=Config.URL_BASE_PATH)

# Create instance of dash server
server = dash_app.server

Created a new entry_point.py :

import app

from index import flask_app

from index import run

if __name__ == "__main__":
    # Main entry point for app
    run()

web.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <system.webServer>
        <handlers>
            <remove name="PythonHandler"/>
            <add name="PythonHandler" path="*" verb="*" modules="FastCgiModule" scriptProcessor="c:python3_8_1python.exe|c:python3_8_1Libsite-packageswfastcgi.py"
                resourceType="Unspecified" requireAccess="Script"/>
        </handlers>
    </system.webServer>
    <appSettings>
        <add key="PYTHONPATH" value="D:_MyPythonTCO_WIP2TCO"/>
        <add key="WSGI_HANDLER" value="entry_point.flask_app"/>
        <add key="WSGI_LOG" value="D:_MyPythonTCO_WIP2TCOLogTCO_WIP2.log"/>
    </appSettings>  
</configuration>

Thanks,

Shaul

Answered by Shaul.Sondak on January 1, 2022

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP