Windows 2022
Sponsored Link

IIS : Python スクリプトを使用する2022/01/26

 
IIS 上で Python スクリプトを使用できるよう設定します。
[1]
[2] PowerShell を管理者権限で起動して設定します。
Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.

# CGI および ISAPI 拡張の機能をインストール
PS C:\Users\Administrator> Install-WindowsFeature Web-CGI,Web-ISAPI-Ext 

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

# 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:
RX-7.srv.world   2    Started    C:\inetpub\newsite             http *:80:rx-7.srv.world
                                                                https *:443:rx-7.srv.world 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

# Python を ISAPI/CGI 拡張へ登録
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"} 

# 例として [RX-7.srv.world] サイトの [content01] フォルダー配下で [*.py] スクリプト実行可能に設定
PS C:\Users\Administrator> Set-WebConfigurationProperty -Filter '/system.webServer/handlers' -Location "RX-7.srv.world/content01" -Name accessPolicy -Value "Read, Script" 

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

# Web サイト再起動
PS C:\Users\Administrator> Restart-WebItem -PSPath 'IIS:\Sites\RX-7.srv.world' 

# テストスクリプト作成
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\test.py -Encoding Default 

# アクセスして確認
PS C:\Users\Administrator> curl.exe https://rx-7.srv.world/content01/test.py 

Python Script Test Page on IIS
IIS : Python スクリプトを使用する (GUI)
 
GUI で設定する場合は以下のように実行します。
[3] [サーバーマネージャー] - [役割と機能の追加] から [Web サーバー (IIS)] の機能で、以下のように [CGI] ([ISAPI 拡張] 未インストールの場合はそちらも合わせて) を選択してチェックを入れます。インストール後は IIS を再起動しておきます。
[4] 例として [RX-7.srv.world] サイトの [content01] フォルダーで Python スクリプトが使用できるように設定します。
左ペインで対象フォルダーを選択して、中央ペインで [ハンドラーマッピング] を開きます。
[5] 右ペインの [スクリプトマップの追加] をクリックします。
[6] [名前] フィールドは任意の名前、その他の項目は下例の通りに入力して OK します。
(Python の実行パスが異なる場合は自身の環境に合わせて置き換え)
[7] [はい] をクリックします。
[8] [*.py] ファイルのハンドラーマッピングが追加されました。
[9] 設定したフォルダー配下に Python テストスクリプトを作成して動作確認します。
[10] Web ブラウザーで対象スクリプトにアクセスしてページが表示されれば OK です。
関連コンテンツ