Windows 2022
Sponsored Link

IIS : Use Python scripts2022/01/26

Configure IIS to be able to use Python scripts.
[2] Run PowerShell with Admin Privilege and Configure.
Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.

# Install CGI and ISAPI extension feature
PS C:\Users\Administrator> Install-WindowsFeature Web-CGI,Web-ISAPI-Ext 

Success Restart Needed Exit Code      Feature Result
------- -------------- ---------      --------------
True    No             Success        {CGI}

# restart IIS
PS C:\Users\Administrator> Restart-Service W3SVC 

PS C:\Users\Administrator> Get-Website 

Name             ID   State      Physical Path                  Bindings
----             --   -----      -------------                  --------
Default Web Site 1    Started    %SystemDrive%\inetpub\wwwroot  http *:80:   2    Started    C:\inetpub\newsite             http *
                                                                https * sslFlags=0

PS C:\Users\Administrator> Get-ChildItem C:\inetpub\newsite 

    Directory: C:\inetpub\newsite

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d-----         1/24/2022  11:03 PM                aspnet_client
d-----         1/24/2022  11:09 PM                auth_basic
d-----         1/24/2022  11:22 PM                auth_win
d-----         1/24/2022  11:25 PM                content01
d-----         1/24/2022  11:26 PM                content02
-a----         1/24/2022  11:06 PM            428 index.aspx
-a----         1/24/2022   9:55 PM             28 index.html

# add Python to ISAPI/CGI extension
PS C:\Users\Administrator> Add-WebConfiguration -Filter '/system.webServer/security/isapiCgiRestriction' -Value @{description="Python";path="C:\Program Files\Python310\python.exe %s %s";allowed="true"} 

# for example, set [*.py] scripts as executable under [content01] folder on [] site
PS C:\Users\Administrator> Set-WebConfigurationProperty -Filter '/system.webServer/handlers' -Location "" -Name accessPolicy -Value "Read, Script" 

PS C:\Users\Administrator> Add-WebConfiguration -Filter '/system.webServer/handlers' -Location "" -Value @{name="Python Interpreter";path="*.py";verb="*";modules="CgiModule";scriptProcessor="C:\Program Files\Python310\python.exe %s %s";resourceType="File"} 

# restart the Web site
PS C:\Users\Administrator> Restart-WebItem -PSPath 'IIS:\Sites\' 

# create a test script
PS C:\Users\Administrator> $str_document = @'
print("Content-type: text/html\n\n")
print("Python Script Test Page on IIS")
PS C:\Users\Administrator> Write-Output $str_document | Out-File C:\inetpub\newsite\content01\ -Encoding Default 

# verify accesses
PS C:\Users\Administrator> curl.exe 

Python Script Test Page on IIS
IIS : Use Python scripts (GUI)
On GUI configuration, set like follows.
[3] Run [Start] - [Server Manager] and enter [Add roles and features], then check a box [CGI] and [ISAPI Extensions] and install them.
[4] For example, set [*.py] scripts as executable under [content01] folder on [] site.
Select target folder on the left pane and Open [Handler Mappings] on the center pane.
[5] Click [Add Script Map] on th right pane.
[6] For the [Name] field, input any name you like, for other fields, input like follows.
(but your Python path is different, replace to your path)
[7] Click [Yes].
[8] [*.py] handler mapping has been added.
[9] Create a Python test script to verify setting.
[10] Access to the test script to verify working.
Matched Content