Google App Engine Python Cron

发布于 2024-11-06 01:23:34 字数 408 浏览 3 评论 0原文

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

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

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

有想法吗?

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

I've been trying for a few days now to get Google App Engine to run a cron Python script which will simply execute a script hosted on a server of mine.

It doesn't need to post any data to the page, simply open a connection, wait for it to finish then email me.

The code I've previously written has logged as "successful" but I never got an email, nor did I see any of the logging.info code I added to test things.

Ideas?

The original and wrong code that I originally wrote can be found at Google AppEngine Python Cron job urllib - just so you know I have attempted this before.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

没企图 2024-11-13 01:23:34

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

首先,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="[email protected]",
            to="[email protected]",
            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()

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

Mix of weird things was happening here.

Firstly, app.yaml I had to place my /cron handler before the root was set:

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

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

Otherwise I'd get crazy errors about not being able to find the file. That bit actually makes sense.

The next bit was the Python code. Not sure what was going on here, but in the end I managed to get it working by doing this:

#!/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="[email protected]",
            to="[email protected]",
            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()

Whatever it was causing the problems, it's now fixed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文