首页 技术 正文
技术 2022年11月18日
0 收藏 762 点赞 3,506 浏览 2438 个字

问题描述

当创建一个App Service 后,运行时环境和版本选择Windows 和 Python 3.6. 登录Kudu 站点查看,默认的文件有 web.config, hostingstart-python.py, hostingstart-python.html,  在配置文件中,通过pythonpath来指定启动目录,而 WSGI_HANDLER 则指定启动的py文件为 hostingstart-python.py.

web.config

<configuration>
<appSettings>
<add key="pythonpath" value="%SystemDrive%\home\site\wwwroot" />
<add key="WSGI_HANDLER" value="hostingstart-python.application" />
</appSettings>
</configuration>

hostingstart-python.py 文件中定义了应用的返回内容为hostingstart-python.html中的内容

import sys
import platformdef application(environ, start_response):
start_response(b'200 OK', [(b'Content-Type', b'text/html')])
with open ("hostingstart-python.html", "r") as hostingstart_file:
hosting = hostingstart_file.read()
yield hosting.encode('utf8').replace(b'PYTHON_VERSION', platform.python_version().encode('utf8'))

hostingstart-python.html 

<html>
<body>test from other root folder start Python project from wwwroot....</body>
</html>

当访问站点时候,就会把 hostingstart-python.html 中的内容显示到首页。但是当站点中也需要部署一些静态html文件时,发现不管如何修改URL,始终返回的内容都是hostingstart-python.html 。

由于Python应用的启动文件中强制返回的内容为hostingstart-python.html,而且没有配置route,所以不管是什么URL访问到此站点,永远输出都是同样内容,因为处理请求的进程是Python.exe, 而非w3wp.exe

问题解决

如何使用IIS来处理静态页面的请求呢?实现Python 站点也能通过URL访问到正确的静态文件(Serving Static Files): 有的。在静态文件夹中添加 web.config 文件,并添加以下内容:

<?xml version="1.0"?>
<configuration>
<system.webServer>
<handlers>
<clear />
<add
name="StaticFile"
path="*" verb="*" modules="StaticFileModule,DefaultDocumentModule,DirectoryListingModule"
resourceType="Either"
requireAccess="Read" />
</handlers>
</system.webServer>
</configuration>
  • 它告诉IIS这是一个静态资源文件,只需要StaticFileModule 等就可以解析,不需要使用Python wfastcgi模块

注意:添加web.config文件后,需要重启站点(App Service)。 然后就可以自由查看静态页面内容。

附录:另外也可以通过在wwwroot目录中的web.config中配置URL重写的规则,来实现对静态文件的访问

添加如下的Rewrite规则:

<system.webServer>
<rewrite>
<rules>
<rule name="Static Files" stopProcessing="true">
<conditions>
<add input="true" pattern="false" />
</conditions>
</rule>
<rule name="Configure Python" stopProcessing="true">
<match url="(.*)" ignoreCase="false" />
<conditions>
<add input="{REQUEST_URI}" pattern="^/static/.*" ignoreCase="true" negate="true" />
</conditions>
<action type="Rewrite" url="handler.fcgi/{R:1}" appendQueryString="true" />
</rule>
</rules>
</rewrite>
</system.webServer>

参考资料

Django app with HttpPlatformHandler in Azure App Services (Windows) (Serving Static Files): https://azureossd.github.io/2017/09/01/django-app-with-httpplatformhandler-in-azure-app-services-windows/

如何在 Azure 应用服务 (Windows) 上设置 Python 环境https://docs.microsoft.com/zh-cn/visualstudio/python/managing-python-on-azure-app-service?view=vs-2019

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,104
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,581
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,428
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,200
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,835
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,918