Python web應用想要發佈使用iis發佈有兩種方式,這篇文章就為大家介紹一下這兩種方式的具體實現: 1.配置HttpPlatform程式 HttpPlatform 模塊將套接字連接直接傳遞到獨立的 Python 進程。 藉助此傳遞可根據需要運行任何 Web 伺服器,但需要用於運行本地 Web ...
Python web應用想要發佈使用iis發佈有兩種方式,這篇文章就為大家介紹一下這兩種方式的具體實現:
1.配置HttpPlatform程式
HttpPlatform 模塊將套接字連接直接傳遞到獨立的 Python 進程。 藉助此傳遞可根據需要運行任何 Web 伺服器,但需要用於運行本地 Web 伺服器的啟動腳本。 在 web.config 的 <httpPlatform> 元素中指定腳本,其中 processPath 屬性指向站點擴展的 Python 解釋器,arguments 屬性指向腳本和希望提供的任何參數:
<?xml version="1.0" encoding="utf-8"?> <configuration> <system.webServer> <handlers> <add name="PythonHandler" path="*" verb="*" modules="httpPlatformHandler" resourceType="Unspecified"/> </handlers> <httpPlatform processPath="c:\python36-32\python.exe" arguments="c:\home\site\wwwroot\runserver.py --port %HTTP_PLATFORM_PORT%" stdoutLogEnabled="true" stdoutLogFile="c:\home\LogFiles\python.log" startupTimeLimit="60" processesPerApplication="16"> <environmentVariables> <environmentVariable name="SERVER_PORT" value="%HTTP_PLATFORM_PORT%" /> </environmentVariables> </httpPlatform> </system.webServer> </configuration>
此處顯示的 HTTP_PLATFORM_PORT 環境變數包含埠,本地伺服器使用該埠偵聽來自 localhost 的連接。 此示例還演示如何根據需要創建其他環境變數,本示例中為 SERVER_PORT。
關於httplplatform的更多描述可以參考https://docs.microsoft.com/en-us/iis/extensions/httpplatformhandler/httpplatformhandler-configuration-reference
2.配置 FastCGI 處理程式
FastCGI 是在請求級別工作的介面。 IIS 接收傳入的連接,並將每個請求轉發到在一個或多個持久 Python 進程中運行的 WSGI 應用。
若要使用 wfastcgi 包,請先安裝並配置它,如 pypi.org/project/wfastcgi/ 所述。
接下來,將應用的 web.config 文件修改為,在 PythonHandler 鍵中添加 python.exe 和 wfastcgi.py 的完整路徑。
- 修改 web.config 中的 PythonHandler 條目,讓路徑與 Python 安裝位置一致(有關確切的詳細信息,請參閱 IIS 配置參考 (iis.net))。
<system.webServer> <handlers> <add name="PythonHandler" path="*" verb="*" modules="FastCgiModule" scriptProcessor="c:\python36-32\python.exe|c:\python36-32\wfastcgi.py" resourceType="Unspecified" requireAccess="Script"/> </handlers> </system.webServer>
- 在 web.config 的 <appSettings> 部分中,為 WSGI_HANDLER、WSGI_LOG(可選)和 PYTHONPATH 添加鍵:
<appSettings> <add key="PYTHONPATH" value="c:\home\site\wwwroot"/> <!-- The handler here is specific to Bottle; see the next section. --> <add key="WSGI_HANDLER" value="app.wsgi_app()"/> <add key="WSGI_LOG" value="c:\home\LogFiles\wfastcgi.log"/> </appSettings>
PYTHONPATH 的值可以自由擴展,但必須包括你的應用的根目錄,他擴展了sys.path,可以在這個路徑下找到import的包。
WSGI_HANDLER 必須指向可從你的應用導入的 WSGI 應用,針對不同的框架,這個值也有一些區別,下麵是一些例子。
1.Bottle:確保 app.wsgi_app 後面有括弧,如下所示。 此操作是必需的,因為該對象是函數(請參閱 app.py))而非變數:
<!-- Bottle apps only --> <add key="WSGI_HANDLER" value="app.wsgi_app()"/>
2.Flask:將 WSGI_HANDLER 值更改為 <project_name>.app,其中 <project_name> 與項目名稱匹配。 可通過查看 runserver.py 中的 from <project_name> import app 語句,找到準確的標識符。 例如,如果項目命名為“FlaskAzurePublishExample”,則該條目如下所示:
<!-- Flask apps only: change the project name to match your app --> <add key="WSGI_HANDLER" value="flask_iis_example.app"/>
3.Django:對於 Django 項目,需要對“web.config”進行兩項更改。 首先,將 WSGI_HANDLER 值更改為 django.core.wsgi.get_wsgi_application()(該對象位於 wsgi.py 文件中):
<!-- Django apps only --> <add key="WSGI_HANDLER" value="django.core.wsgi.get_wsgi_application()"/>
其次,在 WSGI_HANDLER
條目下添加以下條目,並將 DjangoAzurePublishExample
替換為項目名稱:
<add key="DJANGO_SETTINGS_MODULE" value="django_iis_example.settings" />
WSGI_LOG 為可選,但建議在調試應用時使用,記錄日誌。
以上就是這兩種方式,但是作為補充我還是想跟大家分享一下第二種方式,使用fastcgi時,我們在安裝完wfastcgi後輸入命令wfastcgi-enable之後程式做了什麼。
我們可以根據IIS文檔中對於FastCGI節的描述瞭解到。如果我們想要在web.config使用fastCGI時,必須先定義了該模塊:
而這個定義方法呢,就是在IIS全局配置ApplicationHost.config中添加下麵的配置,而這個也是我們在輸入wfastcgi-enable之後做的事情:
<?xml version="1.0"?> <configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform"> <system.webServer> <fastCgi> <application fullPath="d:\home\site\wwwroot\Python34\python.exe" xdt:Locator="Match(fullPath)" xdt:Transform="Remove" /> <application fullPath="d:\home\site\wwwroot\Python34\python.exe" arguments="D:\Python34\Scripts\wfastcgi.py" maxInstances="0" xdt:Transform="Insert"/> </fastCgi> </system.webServer> </configuration>
如果您遇到了無法使用wfastcgi-enable這個命令的情況,比如Azure web app的windows環境,那麼你可以使用這種方式使用自定義的python版本。
參考文檔:
https://docs.microsoft.com/en-us/iis/extensions/httpplatformhandler/httpplatformhandler-configuration-reference
https://docs.microsoft.com/zh-cn/visualstudio/python/configure-web-apps-for-iis-windows?view=vs-2019
https://pypi.org/project/wfastcgi/
https://docs.microsoft.com/en-us/iis/configuration/system.webserver/fastcgi/