Python web在IIS上發佈方法和原理

Python web應用想要發佈使用iis發佈有兩種方式,這篇文章就爲你們介紹一下這兩種方式的具體實現:python

1.配置HttpPlatform程序

HttpPlatform 模塊將套接字鏈接直接傳遞到獨立的 Python 進程。 藉助此傳遞可根據須要運行任何 Web 服務器,但須要用於運行本地 Web 服務器的啓動腳本。 在 web.config 的 <httpPlatform> 元素中指定腳本,其中 processPath 屬性指向站點擴展的 Python 解釋器,arguments 屬性指向腳本和但願提供的任何參數:web

<?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。

django

關於httplplatform的更多描述能夠參考https://docs.microsoft.com/en-us/iis/extensions/httpplatformhandler/httpplatformhandler-configuration-reference flask

2.配置 FastCGI 處理程序

FastCGI 是在請求級別工做的接口。 IIS 接收傳入的鏈接,並將每一個請求轉發到在一個或多個持久 Python 進程中運行的 WSGI 應用。

若要使用 wfastcgi 包,請先安裝並配置它,如 pypi.org/project/wfastcgi/ 所述。

接下來,將應用的 web.config 文件修改成,在 PythonHandler 鍵中添加 python.exe 和 wfastcgi.py 的完整路徑。
windows

  • 修改 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 爲可選,但建議在調試應用時使用,記錄日誌。app

 

以上就是這兩種方式,可是做爲補充我仍是想跟你們分享一下第二種方式,使用fastcgi時,咱們在安裝完wfastcgi後輸入命令wfastcgi-enable以後程序作了什麼。框架

咱們能夠根據IIS文檔中對於FastCGI節的描述瞭解到。若是咱們想要在web.config使用fastCGI時,必須先定義了該模塊:函數

 

而這個定義方法呢,就是在IIS全局配置ApplicationHost.config中添加下面的配置,而這個也是咱們在輸入wfastcgi-enable以後作的事情:ui

<?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/

相關文章
相關標籤/搜索