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的更多描述可以参考
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版本。
参考文档:以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自学编程网。