【Azure 应用服务】Azure App Service (Windows) 使用Flask框架部署Python应用,如何在代码中访问静态文件呢?如何设置文件路径?是相对路径还是绝对路径呢?

简介: 【Azure 应用服务】Azure App Service (Windows) 使用Flask框架部署Python应用,如何在代码中访问静态文件呢?如何设置文件路径?是相对路径还是绝对路径呢?

问题描述

使用Flask框架部署Python代码,如何访问其中的静态文件呢?如static问价夹中的图像资源,同时如何在代码中读取txt文件中的内容呢?是相对路径或者是绝对路径呢?

实验步骤

在App Service (Windows)环境中部署Flask Python应用时,需要考虑注意参考文档“发布到 Windows 上的 Azure 应用服务”, Python项目的Web.config内容为:

<system.webServer>
  <handlers>
    <add name="PythonHandler" path="*" verb="*" modules="FastCgiModule"
        scriptProcessor="D:\home\Python361x64\python.exe|D:\home\Python361x64\wfastcgi.py"
        resourceType="Unspecified" requireAccess="Script"/>
  </handlers>
</system.webServer>

而特别对于Flash项目,需要添加项目的启动配置:

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="FlaskAzurePublishExample.app"/>

以下的实验是在通过相对路径访问静态图片:

  • 首先项目路径如下(项目发布到Azure后,通过Kudu站点查看项目文件路径)

 

  • 静态文件放在了static这个文件夹下
  • test.py运行代码如下:
 
from flask import Flask,render_template
 
app = Flask(__name__, static_folder='', static_url_path='')
@app.route('/')
def index():
    return render_template('index.html')
    
if __name__ == '__main__':
app.run()
  • 访问的时候用url_for函数,templates文件夹下的index.html如下:

  • 测试结果,直接访问站点根目录时可以访问到静态文件

 

 

以上是通过HTML访问项目中的静态资源文件,如果是需要通过代码的方式读取txt文件中的内容,由于可能存在发布后的相对路径与本地测试时候路径不一致,所以建议使用部署在App Service中的绝对路径。如:open(‘D:/hone/site/wwwroot/static/test.txt’,’r’)  

 

但如果一定需要使用相对路径,这可以考虑使用App Service Home路径的环境变量,如:open(‘%HOME%/site/wwwroot/static/test.txt,’r’) 。

 

参考资料

发布到 Windows 上的 Azure 应用服务https://docs.microsoft.com/zh-cn/visualstudio/python/publish-to-app-service-windows?view=vs-2019#publish-to-app-service---visual-studio-2017-and-later

相关文章
|
5天前
【Azure Logic App】使用Event Hub 连接器配置 Active Directory OAuth 认证无法成功连接到中国区Event Hub的解决之法
An exception occurred while retrieving properties for Event Hub: logicapp. Error Message: 'ClientSecretCredential authentication failed: AADSTS90002: Tenant 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' not found. Check to make sure you have the correct tenant ID and are signing into the correct cloud. Che
|
11天前
|
安全
【Azure App Service】App service无法使用的情况分析
App Service集成子网后,如果子网网段中的剩余IP地址非常少的情况下,会在App Service实例升级时( 先加入新实例,然后在移除老实例 )。新加入的实例不能被分配到正确的内网IP地址,无法成功的访问内网资源。 解决方法就是为App Service增加子网地址, 最少需要/26 子网网段地址。
|
20天前
|
C++
【Azure Logic App】使用Event Hub 连接器配置 Active Directory OAuth 认证无法成功连接到中国区Event Hub
【Azure Logic App】使用Event Hub 连接器配置 Active Directory OAuth 认证无法成功连接到中国区Event Hub
|
20天前
【Azure Logic App】在逻辑应用中开启或关闭一个工作流是否会对其它工作流产生影响呢?
【Azure Logic App】在逻辑应用中开启或关闭一个工作流是否会对其它工作流产生影响呢?
|
20天前
|
存储 Linux 开发工具
【Azure App Service】本地Git部署Python Flask应用上云(Azure App Service For Linux)关键错误
【Azure App Service】本地Git部署Python Flask应用上云(Azure App Service For Linux)关键错误
|
20天前
|
存储 SQL JSON
【Azure Logic App】微软云逻辑应用连接到数据库,执行存储过程并转换执行结果为JSON数据
【Azure Logic App】微软云逻辑应用连接到数据库,执行存储过程并转换执行结果为JSON数据
【Azure Logic App】微软云逻辑应用连接到数据库,执行存储过程并转换执行结果为JSON数据
|
20天前
|
安全 网络安全 Windows
【Azure App Service】遇见az命令访问HTTPS App Service 时遇见SSL证书问题,暂时跳过证书检查的办法
【Azure App Service】遇见az命令访问HTTPS App Service 时遇见SSL证书问题,暂时跳过证书检查的办法
【Azure App Service】遇见az命令访问HTTPS App Service 时遇见SSL证书问题,暂时跳过证书检查的办法
|
20天前
|
安全 前端开发 网络安全
【Azure App Service】访问App Service应用报错 SSL: WRONG_VERSION_NUMBER
【Azure App Service】访问App Service应用报错 SSL: WRONG_VERSION_NUMBER
|
20天前
|
Java 应用服务中间件 nginx
【Azure Spring Apps】Spring App部署上云遇见 502 Bad Gateway nginx
【Azure Spring Apps】Spring App部署上云遇见 502 Bad Gateway nginx
|
20天前
|
存储 Linux 网络安全
【Azure App Service】.NET代码实验App Service应用中获取TLS/SSL 证书 (App Service Linux/Linux Container)
【Azure App Service】.NET代码实验App Service应用中获取TLS/SSL 证书 (App Service Linux/Linux Container)