Google AppEngine Python Cron作业urllib

问题描述:

我需要使用Google AppEngine设置一个cron作业,它将使用urllib2来执行托管在我的另一台服务器上的网页。Google AppEngine Python Cron作业urllib

我知道脚本正在执行(检查日志),但我的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() 

任何人都可以看到有什么不对吗?

+1

fwiw,你可能最好使用urlfetch API而不是urllib;你有更多的控制权,例如超时,urllib支持的设计真的是与现有的库兼容,而不是直接在你的代码中使用。 – geoffspear 2011-05-06 14:16:33

+0

谢谢Wooble,我会确保我使用它:) – James 2011-05-06 14:19:14

main()应在util.run_wsgi_app(application)后结束。其余的属于你的处理程序类。

+0

即使这样做,我没有得到任何输出。 – James 2011-05-06 10:35:09

+0

输出或记录?您不会向HTTP响应发送任何内容。 – 2011-05-06 10:42:55

+0

在您的处理程序中使用self.response.out.write(msg)来输出内容。 – 2011-05-06 10:44:37

我刚刚使用Google App Engine根目录中的main.py,然后这似乎给了我一个更好的结果。

除了德鲁提到的问题之外,还有一些问题。

+0

您是否可以针对其他问题进行具体说明,以便遇到相同问题的其他人会发现您的问题有帮助? – 2011-05-06 16:38:32