几天来我一直在尝试让 Google App Engine 运行一个 cron Python 脚本,该脚本将简单地执行托管在我的服务器上的脚本。
它不需要向页面发布任何数据,只需打开一个连接,等待它完成然后给我发电子邮件。
我之前编写的代码记录为“成功”,但我从未收到电子邮件,也没有看到我添加用于测试的任何 logging.info
代码。
想法?
可以在 Google AppEngine Python Cron job urllib 找到我最初写的原始和错误代码。 - 只是想让你知道我以前尝试过这个。
最佳答案
这里发生了各种奇怪的事情。
首先,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()
无论是什么原因导致的问题,现在都已修复。
关于python - 谷歌应用引擎 Python Cron,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5942312/