6

我想解決一個狀態405試圖生成報表時,我從任務隊列得到:狀態405從任務隊列

2012-02-16 03:56:53.012 /report/ 405 3ms 0kb AppEngine-Google; (+http://code.google.com/appengine) 

2012-02-16 03:56:53.007 /createreport/ 302 20ms 0kb Mozilla/5.0 (X11; Linux x86_64; rv:2.0) Gecko/20100101 Firefox/4.0 
I 2012-02-16 03:56:52.990 creating report task 

創建任務的代碼是

class CreateReportHandler(webapp2.RequestHandler): 

    def get(self): 
     logging.info('creating report task') 
     taskqueue.add(url=r'/report/') 
     self.redirect('/') 

而且我已經排到與webapp2的:

Route(r'/createreport/', handler=CreateReportHandler, name='createreport'), 

那麼我應該能夠使它成爲一個cron作業,但是當我測試它,我得到405從這段代碼的訪問的時間,如果我嘗試直接運行它:

class Report(webapp2.RequestHandler): 

    def get(self): 
     # Create a conversion request from HTML to PDF. 
     users = User.query() 
     today = date.today() 
     startdate = date(today.year, today.month, 1) # first day of month 
     html = None  
     for user in users: 
      if user.activity() > 0: 
       logging.info('found active user %s %s' % (user.firstname, user.lastname)) 
       html = '<html><body><table border="1">' 
       html = html + '<tr><td>ORDER</td><td colspan="2">----DISTRIBUTOR----</td><td>ORDER</td><td>Silver</td><td>%</td><td>Total</td><td>Bonus</td></tr>' 
       level = user.level() 
       distributor = user 
       while distributor.has_downline(): 
        downline = User.query(User.sponsor == distributor.key).order(User.lastname).fetch() 
        for person in downline: # to this for whole downline 
         orders = model.Order.all().filter('distributor_id =' , person.key.id()).filter('created >' , startdate).filter('status =', 'PAID').fetch(999999) 
         silver = 0 
         name = person.firstname +' '+ person.lastname 
         for order in orders: 
          logging.info('found orders') 
          for idx,item in enumerate(order.items): 
           purchase = model.Item.get_by_id(long(item.id())) 
           amount = int(order.amounts[idx]) 
           silver = silver + amount*purchase.silver/1000.000 
          if len(name) > 13: 
           name = name[13] 
          html = html + '<tr><td>' + str(order.created.date().day)+'/'+ str(order.created.date().month)+'</td><td>' + filters.makeid(person.key.id()) +'</td><td>' + name + '</td><td>' + str(order.key().id()) + '</td><td>' + str(silver) 
          dist_level = order.dist_level 
          bonus = 0 
          if level == 5 and dist_level == 4:       
           bonus = 0.05 
          if level == 5 and dist_level == 3: 
           bonus = 0.1 
          if level == 5 and dist_level == 2: 
           bonus = 0.13 
          if level == 5 and dist_level == 1: 
           bonus = 0.35 

          if level == 4 and dist_level == 3:       
           bonus = 0.05 
          if level == 4 and dist_level == 2: 
           bonus = 0.08 
          if level == 4 and dist_level == 1: 
           bonus = 0.3 

          if level == 3 and dist_level == 2:       
           bonus = 0.03 
          if level == 3 and dist_level == 1: 
           bonus = 0.25 

          if level == 2 and dist_level == 1:       
           bonus = 0.2 

          html = html + '</td><td>' + str(bonus) + '</td><td>' + str(order.total) 
          bonusmoney = bonus * float(order.total) 
          html = html + '</td><td>' + str(bonusmoney) + '</td></tr>' 

         distributor = person 

       html = html + '</table>' 

      asset = conversion.Asset("text/html", html, "test.html") 
      conversion_obj = conversion.Conversion(asset, "application/pdf")   
      rpc = conversion.create_rpc() 
      conversion.make_convert_call(rpc, conversion_obj) 

      result = rpc.get_result() 
      if result.assets: 
       for asset in result.assets: 
        logging.info('emailing report')# to %s' % user.email) 
        message = mail.EmailMessage(sender='[email protected]', 
            subject='Report %s %s' % (user.firstname, user.lastname)) 
        message.body = 'Here is the monthly report' 
        message.to = '[email protected]' 
        message.bcc = '[email protected]' 
        message.attachments = ['report.pdf', asset.data] 
        message.send() 
        logging.info('message sent') 

如何解決狀態405,並通過執行獲得?

謝謝

回答

14

我從GAE/J-地來了,所以我不熟悉Python,但我以前曾遇到從我的任務隊列工人405響應。在我的情況下,這是由於在建立Task時將TaskOption方法設置爲POST而造成的,而我的處理程序僅處理GET請求。

編輯:檢查TaskQueue.add() docs後,如果未指定方法(如代碼示例中所示),則顯示默認方法爲POST,而您的處理程序似乎只能提供GET請求。

我的建議是明確指定您的任務使用GET方法而不是POST,或者將您的處理程序的處理方法更改爲POST而不是GET。

+2

謝謝,我發現它對我來說也是一樣的 - 它使得一個http'POST'而不是'GET' – 2012-02-16 12:00:01

1

只想補充一種可能的情形,因爲一個快速搜索「任務隊列405」都在這個頁面結束:

我得到405錯誤,因爲沒有指定一個「目標」參數。 taskqueue.add()最終將任務添加到默認目標,其中我的處理程序位於單獨的後端模塊上。

如果未指定目標,那麼將在它們入隊的應用程序的相同版本 上調用任務。因此,如果您在默認應用程序版本中入隊了 任務,而未在隊列上指定目標 ,則會在默認應用程序版本中調用該任務。