【问题标题】:Python Application Error on Google App EngineGoogle App Engine 上的 Python 应用程序错误
【发布时间】:2016-07-30 14:24:44
【问题描述】:

我在谷歌应用引擎上创建了 twitter 应用程序使用这个存储库https://github.com/cybermithun/twitter-search-reply-bot 在运行这个应用程序时我收到了这个错误。谁能告诉我代码有什么问题

Internal Server Error

The server has either erred or is incapable of performing the requested operation.

Traceback (most recent call last):
  File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1535, in __call__
    rv = self.handle_exception(request, response, e)
  File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1529, in __call__
    rv = self.router.dispatch(request, response)
  File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1278, in default_dispatcher
    return route.handler_adapter(request, response)
  File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1102, in __call__
    return handler.dispatch()
  File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 572, in dispatch
    return self.handle_exception(e, self.app.debug)
  File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 570, in dispatch
    return method(*args, **kwargs)
  File "/base/data/home/apps/s~leoapp109/1.391952730855998659/main.py", line 32, in get
    lastTweet = db.GqlQuery("SELECT * FROM LastTweet").fetch(1)[0]
IndexError: list index out of range

这是我的代码

#!/usr/bin/env python
#**strong text**
# Copyright 2007 Google Inc.

import datetime
from google.appengine.ext import db
import webapp2
import time
import sys
import random
from twython import Twython, TwythonError
from google.appengine.ext import db

class LastTweet(db.Model):
    tweetId = db.StringProperty()

class MainHandler(webapp2.RequestHandler):
    def get(self):
        idnum = 0
        lastTweet = db.GqlQuery("SELECT * FROM LastTweet").fetch(1)[0]

#        try:
#            lastTweet = LastTweet.all()
#        except:
#            lastTweet = None
#        if lastTweet != None:
#            idnum = lastTweet.tweetId
#        else:
#            idnum = "0"


        apiKey = 'QQQ'
        apiSecret = 'QQQ'
        accessToken = 'QQQ'
        accessTokenSecret = 'QQQ'
        twitter = Twython(apiKey,apiSecret,accessToken,accessTokenSecret)
        message = "Hi, if you Love Wine you will Love to Wear this Wine T-Shirt www.teespring.com/WineT"
        search_results = twitter.search(q="#Winelover", since_id = idnum, count=20)
        print(search_results)
        for tweet in search_results["statuses"]:
            screenname = "@" + tweet["user"]["screen_name"]+" ";
            print tweet["id_str"]
            try:
                #self.response.write('Hello world!')
                twitter.update_status(status=screenname + message, in_reply_to_status_id=tweet["id_str"])
            except TwythonError:
                pass
            idnum = tweet["id_str"]
            print(idnum)
        if search_results:
            lastTweet.tweetId = idnum
            lastTweet.put()

       # self.response.write('Hello world!')

app = webapp2.WSGIApplication([
    ('/', MainHandler)
], debug=True)

【问题讨论】:

  • 看起来您的 fetch 正在返回一个空容器。

标签: python google-app-engine twitter google-cloud-storage bots


【解决方案1】:

traceback 的最后一行标识了导致问题的代码行:

lastTweet = db.GqlQuery("SELECT * FROM LastTweet").fetch(1)[0]

并告诉我们问题所在:

IndexError: 列表索引超出范围

您的代码期望db.GqlQuery("SELECT * FROM LastTweet").fetch(1) 的结果是list,并将该列表的第一个元素(索引0 处的元素)分配给lastTweet。但是列表是空的,所以索引 0 处没有元素,所以你得到一个IndexError

要解决此问题,您需要将代码包装在 try / except 块中以处理异常

try:
    lastTweet = db.GqlQuery("SELECT * FROM LastTweet").fetch(1)[0]
except IndexError:
    lastTweet = None # or whatever

或者,如果您只获取一条推文,似乎就是这种情况,请调用查询的 get 方法来检索推文或无:

lastTweet = db.GqlQuery("SELECT * FROM LastTweet").get()
if lastTweet:
    # do stuff

【讨论】:

  • 嘿,感谢您回答我的问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-03-11
  • 2011-03-29
  • 1970-01-01
  • 1970-01-01
  • 2019-12-06
  • 1970-01-01
  • 2013-10-28
相关资源
最近更新 更多