1

I Want to Host my Python Rest API on Windows 10 IIS server.

First I tried to host a sample application but can not able to that.

my_app.py

from flask import Flask

app = Flask(__name__)

@app.route('/')
def index():
    return 'Hello IIS from Flask framework.'

@app.route('/Hello')
def hello_world():
    return 'Hello World!'
    
if __name__ == '__main__':
    app.run()

web.config

<configuration>  
  <system.webServer>
    <handlers>
        <add name="Python FastCGI"
            path="*"
            verb="*"
            modules="FastCgiModule"
            scriptProcessor="C:\Program Files\Python39\python.exe|C:\Program Files\Python39\lib\site-packages\wfastcgi.py"
            resourceType="Unspecified"
            requireAccess="Script" />
    </handlers>
  </system.webServer>
  <appSettings>
    <add key="WSGI_HANDLER" value="my_app.app" /> <!-- {name_of_file}.{name_of_flask_app}-->
    <add key="PYTHONPATH" value="C:\inetpub\wwwroot\test" />
    <add key="WSGI_LOG" value="C:\inetpub\wwwroot\test\app.log" />
  </appSettings>
</configuration>  

I have tried the below tutorials but it is not working for me.

  1. https://medium.com/@rajesh.r6r/deploying-a-python-flask-rest-api-on-iis-d8d9ebf886e9

  2. https://www.youtube.com/watch?v=ma1UvzqF82Q&ab_channel=ShobhitWalia

HTTP Error 500.0 - Internal Server Error : see the Error scrennshot

I need help.... Thanks

davidism
  • 121,510
  • 29
  • 395
  • 339
Dev_King
  • 35
  • 1
  • 5

1 Answers1

0

If you want IIS host python application, you need to install python, and then you need to add module mapping in IIS. Here are the steps:

  1. First install python on your computer.

  2. Enable CGI:

enter image description here

After successful installation, you will see "ISAPI and CGI Restrictions" and Handler Mappings:

enter image description here

  1. Enter "ISAPI and CGI Restrictions", add a new ISAPI or CGI Restrictions:

enter image description here

  1. Enter "Handler Mappings", then add Module Mapping(%S %S needs to be added or an error will be reported):

enter image description here

Finally we can successfully access the python Application:

enter image description here

UPDATE:

Can you see python in your Handler Mappings?

enter image description here

This is my web.config:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
</configuration>

There is nothing in web.config. All my settings are for the entire IIS:

enter image description here

Click "Directory Browsing":

enter image description here

Enable it:

enter image description here

Ding Peng
  • 3,702
  • 1
  • 5
  • 8