[英]Python Application Error on Google App Engine
我在运行此应用程序时使用此存储库https://github.com/cybermithun/twitter-search-reply-bot在google app引擎上创建了twitter应用程序,但出现此错误。 谁能告诉我代码有什么问题
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)
追溯的最后一行标识导致问题的代码行:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.