Google AppEngine Python Cron job urllib

2019-08-06 11:53发布

I'm in need of setting up a cron job using Google AppEngine which will use urllib2 to execute a web page hosted on another server of mine.

I know that the script is executing (checked the logs) but my logging inside my python script never seems to be outputted.

#!/usr/bin/env python
from google.appengine.ext import webapp
from google.appengine.ext.webapp import util

import logging
import urllib
import urllib2

class MainHandler(webapp.RequestHandler):
    def get(self):
        logging.info('Starting backups.')


def main():
    application = webapp.WSGIApplication([('/', MainHandler)],
                                         debug=True)
    util.run_wsgi_app(application)
    urlStr = "http://example.com/cron.php"

    request = urllib2.Request(urlStr)
    try:
            response = urllib2.urlopen(request)
            logging.info("Backup complete!")
    except URLError, e:
            logging.error('There was an error %s', e.reason)


if __name__ == '__main__':
    main()

Can anyone see anything wrong with this?

2条回答
你好瞎i
2楼-- · 2019-08-06 12:06

main() should end after util.run_wsgi_app(application). The rest of that belongs in your handler class.

查看更多
\"骚年 ilove
3楼-- · 2019-08-06 12:25

I just used main.py from the Google App Engine root and then this seems to give me a better result.

There was a few problems other than what Drew mentioned too.

查看更多
登录 后发表回答