运行 一个 Python 应用作为 Azure Web 应用
Running an Python App as an Azure Web App
我正在尝试 运行 将 Microsoft Teams BOT 作为 Azure Web 应用程序 - 可以找到完整代码 here
application.py:
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
import sys
import traceback
from datetime import datetime
from aiohttp import web
from aiohttp.abc import HTTPException
from aiohttp.web import Request, Response, json_response
from botbuilder.core import (BotFrameworkAdapterSettings, TurnContext, BotFrameworkAdapter, )
from botbuilder.core.integration import aiohttp_error_middleware
from botbuilder.schema import Activity, ActivityTypes
from bots import EchoBot
from config import DefaultConfig
CONFIG = DefaultConfig()
# Create adapter.
# See https://aka.ms/about-bot-adapter to learn more about how bots work.
SETTINGS = BotFrameworkAdapterSettings(CONFIG.APP_ID, CONFIG.APP_PASSWORD)
ADAPTER = BotFrameworkAdapter(SETTINGS)
# Catch-all for errors.
async def on_error(context: TurnContext, error: Exception):
# This check writes out errors to console log .vs. app insights.
# NOTE: In production environment, you should consider logging this to Azure
# application insights.
print(f"\n [on_turn_error] unhandled error: {error}", file=sys.stderr)
traceback.print_exc()
# Send a message to the user
await context.send_activity("The bot encountered an error or bug.")
await context.send_activity(
"To continue to run this bot, please fix the bot source code."
)
# Send a trace activity if we're talking to the Bot Framework Emulator
if context.activity.channel_id == "emulator":
# Create a trace activity that contains the error object
trace_activity = Activity(
label="TurnError",
name="on_turn_error Trace",
timestamp=datetime.utcnow(),
type=ActivityTypes.trace,
value=f"{error}",
value_type="https://www.botframework.com/schemas/error",
)
# Send a trace activity, which will be displayed in Bot Framework Emulator
await context.send_activity(trace_activity)
ADAPTER.on_turn_error = on_error
# Create the Bot
BOT = EchoBot()
# Listen for incoming requests on /api/messages
async def messages(req: Request) -> Response:
# Main bot message handler.
if "application/json" in req.headers["Content-Type"]:
body = await req.json()
else:
return Response(status=415)
activity = Activity().deserialize(body)
auth_header = req.headers["Authorization"] if "Authorization" in req.headers else ""
response = await ADAPTER.process_activity(activity, auth_header, BOT.on_turn)
if response:
return json_response(data=response.body, status=response.status)
return Response(status=201)
APP = web.Application(middlewares=[aiohttp_error_middleware])
APP.router.add_post("/api/messages", messages)
if __name__ == "__main__":
try:
web.run_app(APP, host="localhost", port=CONFIG.PORT)
except Exception as error:
raise error
在本地 运行ning 时,我可以将端口设置为 8000,例如,将 ngrok 指向本地主机并在我的 bot 频道注册中指定 url,一切正常。
我一直在尽一切努力让我的代码在 Azure 中运行,但我就是无法完成它。
我已将脚本配置为端口 8000 上的 运行 并在应用程序设置中设置参数(我尝试了 PORT 和 WEBSITES_PORT 因为我有 read that sometimes one of the Flags doesn't work:
Application Settings on Azure
日志中仍然显示:
Container mycontainer for site leobot-msbot has exited, failing site
start ERROR - Container mycontainer didn't respond to HTTP pings on
port: 8000, failing site start. See container logs for debugging.
我也尝试了很多启动命令,但就是想不通。
编辑:
找到 https://docs.microsoft.com/en-us/azure/app-service/containers/how-to-configure-python 并添加 "python3.7 -m aiohttp.web -H localhost -P 8000 application:init_func" 作为启动命令并将代码更改为
def init_func(argv):
APP = web.Application(middlewares=[aiohttp_error_middleware])
APP.router.add_post("/api/messages", messages)
return APP
if __name__ == "__main__":
APP = init_func(None)
try:
web.run_app(APP, host="localhost", port=CONFIG.PORT)
except Exception as error:
raise error
然后在日志中它说 -> 看图片:log extract
...
OSError:尝试绑定地址('::1'、8000、0、0)时出现 [Errno 99] 错误:无法分配请求的地址
也许有人有这方面的经验可以帮助我 :)
因此,在设置我的机器人 2 天后,我终于找到了答案。
在 this article and this article from Microsoft.
的帮助下
在设置-配置下我添加了启动命令
python3.7 -m aiohttp.web -H 0.0.0.0 -P 8000 application:init_func
这里重要的是不要使用 localhost 而是 0.0.0.0 !
在我的 application.py 文件中,我如前所述添加了一个初始化函数并将代码更改为
def init_func(argv):
APP = web.Application(middlewares=[aiohttp_error_middleware])
APP.router.add_post("/api/messages", messages)
return APP
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
我正在尝试 运行 将 Microsoft Teams BOT 作为 Azure Web 应用程序 - 可以找到完整代码 here
application.py:
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
import sys
import traceback
from datetime import datetime
from aiohttp import web
from aiohttp.abc import HTTPException
from aiohttp.web import Request, Response, json_response
from botbuilder.core import (BotFrameworkAdapterSettings, TurnContext, BotFrameworkAdapter, )
from botbuilder.core.integration import aiohttp_error_middleware
from botbuilder.schema import Activity, ActivityTypes
from bots import EchoBot
from config import DefaultConfig
CONFIG = DefaultConfig()
# Create adapter.
# See https://aka.ms/about-bot-adapter to learn more about how bots work.
SETTINGS = BotFrameworkAdapterSettings(CONFIG.APP_ID, CONFIG.APP_PASSWORD)
ADAPTER = BotFrameworkAdapter(SETTINGS)
# Catch-all for errors.
async def on_error(context: TurnContext, error: Exception):
# This check writes out errors to console log .vs. app insights.
# NOTE: In production environment, you should consider logging this to Azure
# application insights.
print(f"\n [on_turn_error] unhandled error: {error}", file=sys.stderr)
traceback.print_exc()
# Send a message to the user
await context.send_activity("The bot encountered an error or bug.")
await context.send_activity(
"To continue to run this bot, please fix the bot source code."
)
# Send a trace activity if we're talking to the Bot Framework Emulator
if context.activity.channel_id == "emulator":
# Create a trace activity that contains the error object
trace_activity = Activity(
label="TurnError",
name="on_turn_error Trace",
timestamp=datetime.utcnow(),
type=ActivityTypes.trace,
value=f"{error}",
value_type="https://www.botframework.com/schemas/error",
)
# Send a trace activity, which will be displayed in Bot Framework Emulator
await context.send_activity(trace_activity)
ADAPTER.on_turn_error = on_error
# Create the Bot
BOT = EchoBot()
# Listen for incoming requests on /api/messages
async def messages(req: Request) -> Response:
# Main bot message handler.
if "application/json" in req.headers["Content-Type"]:
body = await req.json()
else:
return Response(status=415)
activity = Activity().deserialize(body)
auth_header = req.headers["Authorization"] if "Authorization" in req.headers else ""
response = await ADAPTER.process_activity(activity, auth_header, BOT.on_turn)
if response:
return json_response(data=response.body, status=response.status)
return Response(status=201)
APP = web.Application(middlewares=[aiohttp_error_middleware])
APP.router.add_post("/api/messages", messages)
if __name__ == "__main__":
try:
web.run_app(APP, host="localhost", port=CONFIG.PORT)
except Exception as error:
raise error
在本地 运行ning 时,我可以将端口设置为 8000,例如,将 ngrok 指向本地主机并在我的 bot 频道注册中指定 url,一切正常。 我一直在尽一切努力让我的代码在 Azure 中运行,但我就是无法完成它。 我已将脚本配置为端口 8000 上的 运行 并在应用程序设置中设置参数(我尝试了 PORT 和 WEBSITES_PORT 因为我有 read that sometimes one of the Flags doesn't work: Application Settings on Azure
日志中仍然显示:
Container mycontainer for site leobot-msbot has exited, failing site start ERROR - Container mycontainer didn't respond to HTTP pings on port: 8000, failing site start. See container logs for debugging.
我也尝试了很多启动命令,但就是想不通。 编辑: 找到 https://docs.microsoft.com/en-us/azure/app-service/containers/how-to-configure-python 并添加 "python3.7 -m aiohttp.web -H localhost -P 8000 application:init_func" 作为启动命令并将代码更改为
def init_func(argv):
APP = web.Application(middlewares=[aiohttp_error_middleware])
APP.router.add_post("/api/messages", messages)
return APP
if __name__ == "__main__":
APP = init_func(None)
try:
web.run_app(APP, host="localhost", port=CONFIG.PORT)
except Exception as error:
raise error
然后在日志中它说 -> 看图片:log extract ... OSError:尝试绑定地址('::1'、8000、0、0)时出现 [Errno 99] 错误:无法分配请求的地址
也许有人有这方面的经验可以帮助我 :)
因此,在设置我的机器人 2 天后,我终于找到了答案。 在 this article and this article from Microsoft.
的帮助下在设置-配置下我添加了启动命令
python3.7 -m aiohttp.web -H 0.0.0.0 -P 8000 application:init_func
这里重要的是不要使用 localhost 而是 0.0.0.0 !
在我的 application.py 文件中,我如前所述添加了一个初始化函数并将代码更改为
def init_func(argv):
APP = web.Application(middlewares=[aiohttp_error_middleware])
APP.router.add_post("/api/messages", messages)
return APP
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