Skip to main content
 首页 » 编程设计

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

2025年05月04日41davidwang456

一周前我遇到了 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 sockets或 URL fetch服务作为替代方案。