Google AppEngine Python 定时任务 urllib

1 投票
2 回答
733 浏览
提问于 2025-04-16 17:06

我需要在Google AppEngine上设置一个定时任务,这个任务会使用urllib2来访问我另一个服务器上的网页。

我知道这个脚本是有在运行的(我查看了日志),但是我在Python脚本里写的日志信息似乎从来没有输出过。

#!/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()

有没有人能帮我看看这有什么问题吗?

2 个回答

0

我刚刚使用了来自Google App Engine根目录的main.py,这样似乎得到了更好的结果。

除了Drew提到的问题,还有一些其他的问题。

2

main() 这个函数应该在 util.run_wsgi_app(application) 之后结束。后面的内容应该放在你的处理类里。

撰写回答