问题描述

使用Python编写了ChatBot,在部署到App Service,却无法启动。 通过高级工具(Kudu站点:https://<your site name>.scm.chinacloudsites.cn/newui)查看日志显示:Failed to find attribute 'app' in 'app'.

2024-10-25T02:43:29.242073529Z    _____
2024-10-25T02:43:29.242126029Z / _ \ __________ _________ ____
2024-10-25T02:43:29.242132529Z / /_\ \\___ / | \_ __ \_/ __ \
2024-10-25T02:43:29.242136329Z / | \/ /| | /| | \/\ ___/
2024-10-25T02:43:29.242139929Z \____|__ /_____ \____/ |__| \___ >
2024-10-25T02:43:29.242144329Z \/ \/ \/
2024-10-25T02:43:29.242147829Z A P P S E R V I C E O N L I N U X
2024-10-25T02:43:29.242151329Z
2024-10-25T02:43:29.242154629Z Documentation: http://aka.ms/webapp-linux
2024-10-25T02:43:29.242157929Z Python 3.9.19
2024-10-25T02:43:29.242161329Z Note: Any data outside '/home' is not persisted
2024-10-25T02:43:30.929950845Z Starting OpenBSD Secure Shell server: sshd.
2024-10-25T02:43:30.957953290Z WEBSITES_INCLUDE_CLOUD_CERTS is not set to true.
2024-10-25T02:43:31.046614933Z Updating certificates in /etc/ssl/certs...
2024-10-25T02:43:47.356555353Z 1 added, 0 removed; done.
2024-10-25T02:43:47.363583943Z Running hooks in /etc/ca-certificates/update.d...
2024-10-25T02:43:47.381370217Z done.
2024-10-25T02:43:47.458519508Z CA certificates copied and updated successfully.
2024-10-25T02:43:47.764113974Z App Command Line not configured, will attempt auto-detect
2024-10-25T02:43:47.766294671Z Launching oryx with: create-script -appPath /home/site/wwwroot -output /opt/startup/startup.sh -virtualEnvName antenv -defaultApp /opt/defaultsite
2024-10-25T02:43:48.070131739Z Found build manifest file at '/home/site/wwwroot/oryx-manifest.toml'. Deserializing it...
2024-10-25T02:43:48.117325272Z Build Operation ID: 1a78e454a2c951e6
2024-10-25T02:43:48.219390927Z Output is compressed. Extracting it...
2024-10-25T02:43:48.219435627Z Extracting '/home/site/wwwroot/output.tar.gz' to directory '/tmp/8dcf49cf7434c99'...
2024-10-25T02:43:48.221347324Z Oryx Version: 0.2.20240619.2, Commit: cf006407a02b225f59dccd677986973c7889aa50, ReleaseTagName: 20240619.2
2024-10-25T02:43:58.209300259Z App path is set to '/tmp/8dcf49cf7434c99'
2024-10-25T02:44:00.187313274Z Detected an app based on Flask
2024-10-25T02:44:00.187408174Z Generating `gunicorn` command for 'app:app'
2024-10-25T02:44:00.433932149Z Writing output script to '/opt/startup/startup.sh'
2024-10-25T02:44:00.699343416Z Using packages from virtual environment antenv located at /tmp/8dcf49cf7434c99/antenv.
2024-10-25T02:44:00.706651312Z Updated PYTHONPATH to '/opt/startup/app_logs:/tmp/8dcf49cf7434c99/antenv/lib/python3.9/site-packages'
2024-10-25T02:44:03.601118861Z [2024-10-25 02:44:03 +0000] [1064] [INFO] Starting gunicorn 22.0.0
2024-10-25T02:44:03.744989087Z [2024-10-25 02:44:03 +0000] [1064] [INFO] Listening at: http://0.0.0.0:8000 (1064)
2024-10-25T02:44:03.746948887Z [2024-10-25 02:44:03 +0000] [1064] [INFO] Using worker: sync
2024-10-25T02:44:03.849806606Z [2024-10-25 02:44:03 +0000] [1067] [INFO] Booting worker with pid: 1067
2024-10-25T02:44:10.688674133Z Failed to find attribute 'app' in 'app'.
2024-10-25T02:44:10.696985330Z [2024-10-25 02:44:10 +0000] [1067] [INFO] Worker exiting (pid: 1067)
2024-10-25T02:44:11.222634199Z [2024-10-25 02:44:11 +0000] [1064] [ERROR] Worker (pid:1067) exited with code 4
2024-10-25T02:44:11.222689699Z [2024-10-25 02:44:11 +0000] [1064] [ERROR] Shutting down: Master
2024-10-25T02:44:11.222696699Z [2024-10-25 02:44:11 +0000] [1064] [ERROR] Reason: App failed to load.

问题解答

根据下面的步骤修改app.py代码并设置App Service的启动命令。

第一步 : 在 app.py 中添加 init_func 函数

Python ChatBot的实例代码下载地址(创建机器人Python版:https://docs.azure.cn/zh-cn/bot-service/bot-service-quickstart-create-bot?view=azure-bot-service-4.0&tabs=python%2Cvs#create-a-bot)

app.py

# Create the Bot
BOT = EchoBot() # Listen for incoming requests on /api/messages
async def messages(req: Request) -> Response:
return await ADAPTER.process(req, BOT) ## 从这里开始修改,添加 init_func 启动函数 def init_func(argv):
APP = web.Application(middlewares=[aiohttp_error_middleware])
APP.router.add_post("/api/messages", messages)
return APP # APP = web.Application(middlewares=[aiohttp_error_middleware])
# APP.router.add_post("/api/messages", messages) if __name__ == "__main__":
APP = init_func(None)
try:
web.run_app(APP, host="0.0.0.0", port=CONFIG.PORT)
except Exception as error:
raise error

第二步:在Config.py中添加配置项和部署到App Service中

添加的配置项是与中国区Azure Bot Service进行认证的配置项,说明需要参考如下两部分内容:

1: 机器人标识信息 : https://docs.azure.cn/zh-cn/bot-service/v4sdk/bot-builder-authentication?view=azure-bot-service-4.0&tabs=singletenant%2Caadv2%2Cpython#bot-identity-information

2: 机器人在中国区的身份验证设置 : https://docs.azure.cn/zh-cn/bot-service/how-to-deploy-china-cloud?view=azure-bot-service-4.0&tabs=javascript#configure-userassignedmsisingletenant-bot

config.py

#!/usr/bin/env python3
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. import os """ Bot Configuration """ class DefaultConfig:
""" Bot Configuration """
PORT = 3978
APP_ID = os.environ.get("MicrosoftAppId", "app id")
APP_PASSWORD = os.environ.get("MicrosoftAppPassword", "secret")
APP_TYPE = os.environ.get("MicrosoftAppType", "SingleTenant")
APP_TENANTID = os.environ.get("MicrosoftAppTenantId", "tenant id") OAUTH_URL = os.environ.get("OAuthUrl", "https://token.botframework.azure.cn/")
TO_BOT_FROM_CHANNEL_TOKEN_ISSUER = os.environ.get("ToBotFromChannelTokenIssuer", "https://api.botframework.azure.cn") TO_BOT_FROM_CHANNEL_OPENID_METADATA_URL = os.environ.get("ToBotFromChannelOpenIdMetadataUrl", "https://login.botframework.azure.cn/v1/.well-known/openidconfiguration")
TO_BOT_FROM_EMULATOR_OPENID_METADATA_URL = os.environ.get("ToBotFromEmulatorOpenIdMetadataUrl", "https://login.partner.microsoftonline.cn/a55a4d5b-9241-49b1-b4ff-befa8db00269/v2.0/.well-known/openid-configuration")
VALIDATE_AUTHORITY = os.environ.get("ValidateAuthority", "true")
TO_CHANNEL_FROM_BOT_LOGIN_URL = os.environ.get("ToChannelFromBotLoginUrl","https://login.partner.microsoftonline.cn/<tenant id>")
TO_CHANNEL_FROM_BOT_OAUTH_SCOPE = os.environ.get("ToChannelFromBotOAuthScope", "https://api.botframework.azure.cn")

requirements.txt:

botbuilder-integration-aiohttp>=4.15.0
aiohttp
botbuilder-core
botbuilder-schema

修改完成后,部署Python应用到App Service。

第三步: 为App Service添加启动命令

进入App Service配置页面,设置启动命令。

python3 -m aiohttp.web -H 0.0.0.0 -P 8000 app:init_func

修改后,查看日志,应用启动成功!

参考资料

Azure ChatBot Running With Python on an Azure WebApp Not Working :  https://stackoverflow.com/questions/77781014/azure-chatbot-running-with-python-on-an-azure-webapp-not-working

【Azure Bot Service】部署Python ChatBot代码到App Service中的更多相关文章

  1. 【Azure 应用程序见解】 Application Insights 对App Service的支持问题

    问题描述 Web App 发布后, Application Insights 收集不到数据了 问题分析 在应用服务(App Service)中收集应用的监控数据(如Request,Exception, ...

  2. 【应用服务 App Service】快速获取DUMP文件(App Service for Windows(.NET/.NET Core))

    问题情形 当应用在Azure 应用服务App Service中运行时,有时候出现CPU,Memory很高,但是没有明显的5XX错误和异常日志,有时就是有异常但是也不能明确的指出具体的代码错误.当面临这 ...

  3. 【应用服务 App Service】NodeJS +Egg 发布到App Service时遇见 [ERR_SYSTEM_ERROR]: A system error occurred:uv_os_get_passwd returned ENOENT(no such file or directory)

    问题情形 本地NodeJS应用使用Egg脚手架构建,本地运行测试完全没有问题,发布后App Service后不能运行.通过登录到kudu后(https://<your web site>. ...

  4. 【从0開始Tornado建站】0.9版本号python站点代码开源--持续更新中

            从5月份開始[从0開始Tornado建站]这个专栏,開始一点一点把这个分类兴趣站点弄起来,从无到有的过程也是令人兴奋的:-) 国庆的时候等待备案然后上线,如今站点域名为ustchack ...

  5. 【Azure 应用服务】Python flask 应用部署在Aure App Service中作为一个子项目时,解决遇见的404 Not Found问题

    问题描述 在成功的部署Python flask应用到App Service (Windows)后,如果需要把当前项目(如:hiflask)作为一个子项目(子站点),把web.config文件从wwwr ...

  6. 【Azure 应用服务】Azure App Service For Linux 上实现 Python Flask Web Socket 项目 Http/Https

    问题描述 在上篇博文"[Azure 应用服务]App Service for Linux 中实现 WebSocket 功能 (Python SocketIO)"中,实现了通过 HT ...

  7. 【Azure 应用服务】一个 App Service 同时部署运行两个及多个 Java 应用程序(Jar包)

    问题描述 如何在一个AppService下同时部署运行多个Java 应用程序呢? 问题解答 因为App Service的默认根目录为 wwwroot.如果需要运行多个Java 应用程序,需要在 www ...

  8. 【Azure 应用服务】NodeJS Express + MSAL 应用实现AAD集成登录并部署在App Service Linux环境中的实现步骤

    问题描述 实现部署NodeJS Express应用在App Service Linux环境中,并且使用Microsoft Authentication  Library(MSAL)来实现登录Azure ...

  9. 【Azure 应用服务】PHP应用部署在App Service for Linux环境中,上传文件大于1MB时,遇见了413 Request Entity Too Large 错误的解决方法

    问题描述 在PHP项目部署在App Service后,上传文件如果大于1MB就会遇见 413 Request Entity Too Large 的问题. 问题解决 目前这个问题,首先需要分析应用所在的 ...

  10. 【Azure 应用服务】App Service For Linux 部署PHP Laravel 项目,如何修改首页路径为 wwwroot\public\index.php

    问题描述 参考官方文档部署 PHP Laravel 项目到App Service for Linux环境中,但是访问应用时候遇见了500 Server Error 错误. 从部署的日志中,可以明确看出 ...

随机推荐

  1. 与LLMs进行在IDE中直接、无需提示的交互是工具构建者探索的一个有希望的未来方向

    这个观点在卡内基梅隆大学与谷歌研究人员合作文章 <Using an LLM to Help With Code Understanding> 中提出. 论文地址:https://dl.ac ...

  2. milvus 结果

    milvus (2.3.3) 两个查询方法 collection.query(...) 和 collection.search(...) 的返回类型是不同的,用错了会说 attribute error ...

  3. mybatis坑之数字字符串比对

    在mybatis开发过程中有需要在sql中判断查询哪张表,如下sql: SELECT a.tag_name, a.tag_id, count( 0 ) AS base_total FROM mm_dd ...

  4. [nRF24L01+] 1. 硬件设计

    nRF24L01pluss_REFERENCE_MODULES.pdf nrf24l01p_product_specification_1_0.pdf 1. 硬件设计 1.1. 实物图 [左边是PCB ...

  5. 让你的C程序,可以自动更新版本信息

    一.软件管理 稍微上点规模的软件开发往往周期都非常长, 中间会产生很多临时版本, 这些临时版本往往会有各种各样的bug, 由于项目参与的人员众多.水平参差不齐, 软件分支众多.功能复杂, 经常会有各种 ...

  6. 谈谈 Nginx 那点事【二】

    前言 在上一讲 谈谈 Nginx 那点事[一] 中,介绍了Nginx的安装及基本结构,今天将工作中Nginx的一些配置,及常用的场景做一些总结. 这一讲总结的内容主要是关于Nginx服务配置.静态资源 ...

  7. 【Java】之获取CSV文件数据以及获取Excel文件数据

    一.获取CSV文件数据 import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Sheet; impor ...

  8. 2024DASCTF

    DASCTF prese 一眼控制了平坦化,可以用d810梭一下 跟进一下main_crypto这个函数 主要是两部分,第一部分是生成一个256大小的数组,通过输入的长度和遍历生成的一个数组 第二部分 ...

  9. vue打包后dist的使用

    发现问题 vue项目完成打包出dist后准备打开index.html,发现居然页面是一片空白,f12一片报红. 分析问题 经过多次网上查询后发现这是由于vue打包时,脚手架会帮你配置好大量参数,但其中 ...

  10. 微软azure devops 如何搭建代理池服务器

    如果你的pipeline使用微软azure的服务器进行编译失败,提示你不能进行并行任务的话,可以自己拿一台服务器来搭建编译服务器. 目标是让你的 Organization settings -> ...