【问题标题】:Google App Engine Python Cron谷歌应用引擎 Python Cron
【发布时间】:2011-08-21 23:43:19
【问题描述】:

我这几天一直在尝试让 Google App Engine 运行一个 cron Python 脚本,该脚本将简单地执行托管在我的服务器上的脚本。

它不需要向页面发布任何数据,只需打开一个连接,等待它完成然后给我发电子邮件。

我之前编写的代码已记录为“成功”,但我从未收到电子邮件,也没有看到我添加用于测试的任何 logging.info 代码。

想法?

可以在Google AppEngine Python Cron job urllib 找到我最初编写的原始和错误代码 - 让您知道我以前尝试过。

【问题讨论】:

  • 这不是与您之前的问题完全相同吗?
  • 不,因为我实际上并没有让 Cron 作业运行,我实际上让日志说它在运行中是“成功”的。但它什么也没做。
  • 那么从那以后你的代码发生了什么变化?向我们展示您的新代码!另外,当您在浏览器中手动运行脚本时会发生什么?
  • 确实没有。它下载 .py 文件。
  • 下载什么 .py 文件?您其他问题中的代码没有做类似的事情。您是否在另一个问题中实施了已接受答案的建议?你不应该在main() 方法中做真正的工作——一切都应该在处理程序中完成。手动运行脚本会发生什么?

标签: python google-app-engine email cron urllib


【解决方案1】:

这里发生了各种奇怪的事情。

首先,app.yaml 我必须在设置根之前放置我的 /cron 处理程序:

handlers:
- url: /cron
  script: assets/backup/main.py

- url: /
  static_files: assets/index.html
  upload: assets/index.html

否则我会收到关于无法找到文件的疯狂错误。这一点实际上是有道理的。

接下来是 Python 代码。不知道这里发生了什么,但最后我设法通过这样做使其工作:

#!/usr/bin/env python  
# import logging
from google.appengine.ext import webapp
from google.appengine.api import mail
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.api import urlfetch

import logging

class CronMailer(webapp.RequestHandler):
    def get(self):
        logging.info("Backups: Started!")
        urlStr = "http://example.com/file.php"

        rpc = urlfetch.create_rpc()
        urlfetch.make_fetch_call(rpc, urlStr)
        mail.send_mail(sender="example@example.com",
            to="email@example.co.uk",
            subject="Backups complete!",
            body="Daily backups have been completed!")
        logging.info("Backups: Finished!")

application = webapp.WSGIApplication([('/cron', CronMailer)],debug=True)
def main():
    run_wsgi_app(application)

if __name__ == '__main__':
    main()

无论是什么导致了问题,现在都已修复。

【讨论】:

    猜你喜欢
    • 2014-03-22
    • 2019-08-02
    • 2012-06-12
    • 2023-03-12
    • 2014-07-22
    • 2011-01-11
    • 2014-07-20
    • 2016-08-01
    • 1970-01-01
    相关资源
    最近更新 更多