gpt4 book ai didi

python - 在 Google App Engine 上移植基本脚本

转载 作者:太空宇宙 更新时间:2023-11-04 05:44:42 25 4
gpt4 key购买 nike

一周前我遇到了 google app engine 并且想尝试一下它有点。我写了一个虚拟应用程序来检查 url 并基于如果站点“启动”或“关闭”,它会记录到文件中的返回代码

我想将它部署到 App Engine 并安排 cron 作业(例如每分钟一次)。我遇到了这个 example .我完全理解 app.yaml 文件,但我不确定'url' int 他 cron.yaml 文件。目的是什么?可以在文件中跳过吗?

此外,该示例使用了 webapp2 模块。我是否将我的代码包装在该模块中?

代码如下:

import requests
import logging

logger = logging.getLogger('testapp')
hdlr = logging.FileHandler('/Users/me/Desktop/testlog.log')
formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
hdlr.setFormatter(formatter)
logger.addHandler(hdlr)
logger.setLevel(logging.INFO)

try:
r = requests.head("http://www.redcafe.com")
code = (r.status_code)
except requests.ConnectionError:
logger.error('URL Error')

if code == 200:
logger.info('Up and running')
else:
logger.error('Cant find website')

最佳答案

根据cron.yaml doc :

The url field specifies a URL in your application that will be invoked by the Cron Service.

基本上,您的应用程序将为该 URL 注册一个处理程序,当 GAE cron 服务请求它时将调用该处理程序。

您可能想花一些时间至少熟悉一下:

在 GAE 中使用 requests 库可能很棘手:Can Python Requests library be used on Google App Engine?您可能想看看 GAE socketsURL fetch服务作为替代方案。

关于python - 在 Google App Engine 上移植基本脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32722685/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com