Python web publishing methods and principles on IIS

Python web application you want to publish using iis There are two methods, this article will tell you about the specific implementation of these two approaches:

1. Configure HttpPlatform program

HttpPlatform module socket connections directly transferred to a separate Python process. With this transfer may need to run any Web server based, but need for startup scripts to run local Web server. Specified in the web.config <httpPlatform> element in the script, wherein processPath extended attribute points to the site Python interpreter, arguments attribute points to script parameters, and any desired provided:

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

Here HTTP_PLATFORM_PORT display environment variable contains the port, the local server using the port listens for connections from the localhost. This example also shows how to create other environmental variables as needed, in this example SERVER_PORT.

More description of httplplatform can refer https://docs.microsoft.com/en-us/iis/extensions/httpplatformhandler/httpplatformhandler-configuration-reference

Configuring FastCGI handler

FastCGI is the interface at the request level work. IIS receive incoming connections, and each request is forwarded to one or more persistent Python WSGI application running in the process.

To use wfastcgi package, install and configure it as the pypi.org/project/wfastcgi/.

Next, modify the application's web.config file, add the full path python.exe and wfastcgi.py in PythonHandler key.

  • PythonHandler modify entries in web.config, so Python installation path consistent with the position (For more information on the exact, see the IIS configuration reference (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>
  • In the web.config <appSettings> section, as WSGI_HANDLER, WSGI_LOG (optional) and add key 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 can extend the value of freedom, but you must include the root directory of the application, he expanded the sys.path, you can be found in this package import path.
WSGI_HANDLER must point WSGI applications that can be imported from your applications, for different framework, this value there are some differences, here are some examples.

1.Bottle: app.wsgi_app ensure parentheses behind, as shown below. This action is necessary because the object is a function (See app.py)) instead of variables:

<!-- Bottle apps only -->
<add key="WSGI_HANDLER" value="app.wsgi_app()"/>

2.Flask: The WSGI_HANDLER change the value <project_name> .app, where <project_name> project name match. By viewing runserver.py in from <project_name> import app statement, find the exact identifier. For example, if the item name "FlaskAzurePublishExample", then the entry is as follows:

<!-- Flask apps only: change the project name to match your app -->
<add key="WSGI_HANDLER" value="flask_iis_example.app"/>

3.Django: For Django project, the need for "web.config" make two changes. First, change the value to WSGI_HANDLER django.core.wsgi.get_wsgi_application () (the object is located wsgi.py file):

<!-- Django apps only -->
<add key="WSGI_HANDLER" value="django.core.wsgi.get_wsgi_application()"/>

Second, WSGI_HANDLERadd the following entry under the entry and DjangoAzurePublishExamplereplaced with the name of the project:

<add key="DJANGO_SETTINGS_MODULE" value="django_iis_example.settings" />

WSGI_LOG is optional, but recommended when debugging applications logging.

 

These are the two, but as a supplement I would like to share second way, when using fastcgi, we enter the following command wfastcgi-enable the program after installing wfastcgi done.

We can understand the description of IIS FastCGI section according to the document. If we want to use fastCGI in web.config, you must first define the module:

 

而这个定义方法呢,就是在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/

 

Guess you like

Origin www.cnblogs.com/junshijie/p/12105095.html