I'm trying to setup FastAPI in IIS using hypercorn and I keep getting the error
PermissionError: [WinError 10013] An attempt was made to access a socket in a way forbidden by its access permissions
I followed these steps https://github.com/tiangolo/fastapi/discussions/4207#discussioncomment-2216634
My web.config is the following
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<handlers accessPolicy="Read, Execute, Script">
<remove name="httpPlatformHandler"/>
<add name="httpPlatformHandler" path="*" verb="*" modules="httpPlatformHandler" resourceType="Unspecified" />
</handlers>
<httpPlatform processPath="C:\inetpub\wwwroot\FastAPI\Scripts\python.exe"
arguments="-m hypercorn app.main:app --bind 127.0.0.1:8080 --keep-alive 5 --worker-class asyncio --workers 4"
stdoutLogEnabled="true" stdoutLogFile="C:\logs\python.log" startupTimeLimit="120" requestTimeout="00:05:00"
startupRetryCount="3" processesPerApplication="1">
</httpPlatform>
<httpErrors errorMode="Detailed" />
</system.webServer>
</configuration>
If I stop IIS and I run the command below it works fine, it's only when running it inside IIS.
C:\inetpub\wwwroot\FastAPI\Scripts\python.exe -m hypercorn app.main:app --bind 127.0.0.1:8080 --keep-alive 5 --worker-class asyncio --workers 4
I also tried to grant IIS_IUSRS access to python.exe but it didn't make any difference, it was suggested in this post as when calling the API it hangs forever
I suspect it's something to do with the IIS port binding, if I change the web.config to point at another port (e.g. 8000) different than the binding port in IIS then I'm able to access the API (e.g. by navigating to http://localhost:8000), it feels to me IIS is using the port when hypercorn is trying to use it
This turned out to be an issue with hypercorn (I raised an issue in their github repo https://github.com/pgjones/hypercorn/issues/231)
I switched to use uvicorn as @Lex Li suggested following his blog post and it's now working fine