Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run Windows IIS on top of an ASGI server like hypercorn or uvicorn?

I have an api based web application written in python using FastApi which uses Uvicorn or Hypercorn for deployment.These both are ASGI based servers. Is there a way to run IIS on top of this ?

like image 441
Lak Hinsu Avatar asked May 20 '20 17:05

Lak Hinsu


People also ask

What is Uvicorn used for?

Uvicorn is an ASGI web server implementation for Python. Until recently Python has lacked a minimal low-level server/application interface for async frameworks. The ASGI specification fills this gap, and means we're now able to start building a common set of tooling usable across all async frameworks.

How do I use Uvicorn on Windows?

Copy the "uvicorn" and "uvicorn-X. XX. Xdist-info" folders, then go to users/AppData/roaming/Python/Python37/Scripts and copy the "uvicorn.exe". You are going to paste all three of these items to something like path: "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\Scripts" .

Does FastAPI need Uvicorn?

The main thing you need to run a FastAPI application in a remote server machine is an ASGI server program like Uvicorn. There are 3 main alternatives: Uvicorn: a high performance ASGI server.


1 Answers

You can defintely run via IIS. Here is the two possible options you have (I use Hypercorn):

  1. HTTPPlaformHandler

    <configuration>
        <system.webServer>
         <rewrite>
                <rules>
                    <rule name="HTTPS force" enabled="true" stopProcessing="true">
                        <match url="(.*)" />
                        <conditions>
                            <add input="{HTTPS}" pattern="^OFF$" />
                        </conditions>
                        <action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" />
                    </rule>
                </rules>
            </rewrite>
            <handlers>
                <add name="httpplatformhandler" path="*" verb="*" modules="httpPlatformHandler" resourceType="Unspecified" />
            </handlers>
            <httpPlatform requestTimeout="00:05:00" startupTimeLimit="120" startupRetryCount="3" stdoutLogEnabled="true" stdoutLogFile=".\logs\python-stdout" processPath="[PATH TO YOUR PYTHON EXE]" arguments="-m hypercorn app.main:app -b 127.0.0.1:%HTTP_PLATFORM_PORT% --keep-alive 5 --worker-class asyncio --workers 9">
                <environmentVariables>
                    <environmentVariable name="PORT" value="%HTTP_PLATFORM_PORT%" />
                </environmentVariables>
            </httpPlatform>
        </system.webServer>
    </configuration>
    
  2. AspNetCoreModuleV2 (If you use this one you will have to have a config.py to correctly bind the URL and port.)

     <system.webServer>
       <handlers>
         <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
       </handlers>
       <aspNetCore processPath="[PATH TO PYTHON EXE]" arguments="-m hypercorn app.main:app --config file:config.py" stdoutLogEnabled="true" stdoutLogFile=".\logs\stdout" requestTimeout="00:26:00" /></system.webServer>   </location><system.webServer></system.webServer> </configuration>
    
like image 90
Landon Patterson Avatar answered Nov 12 '22 08:11

Landon Patterson