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/

相关文章
相关标签/搜索