简体   繁体   中英

Google App Engine Python Cron

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.

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="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()

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM