在 Google App Engine 上移植基本脚本

Porting Basic Script on Google App Engine

一周前我遇到了 google 应用引擎,想尝试一下 它有点。我写了一个虚拟应用程序来检查 url 并基于 如果网站是 "up" 或 "down"

,它会记录到文件的 return 代码

我想将其部署到 App Engine 并安排 cron 作业(例如每分钟一次)。 我遇到了这个 example。我完全理解 app.yaml 文件,但我不确定 'url' 整合 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 sockets or URL fetch 服务作为替代方案。