通过AJAX调用时导致错误输入错误

时间:2015-09-05 06:19:11

标签: python ajax tweepy internal-server-error

我正在尝试检索搜索到的用户的推文 。当我在python脚本中添加" import tweepy" 时,它会抛出内部服务器错误 - 500 。如果我评论 "导入tweepy" ,那么它会正常运行。但我需要"导入tweepy" 来运行 twitter API

代码(AJAX)脚本:

$.ajax({
    url: "../views/ajax/search_tweets.py",
    type: "post",
    data: {"query" : "@user_name"},
    success: function(response) {
        console.log(response)
    },
    error: function(xhr, ajaxOptions, thrownError) {
        console.log(xhr.responseText);
    }
})

Python脚本:

#!/usr/bin/python
import sys
import cgi
import json
import traceback
import tweepy         # when I import this, it throws 500 error

class SearchTweets(object):

    def __init__(self):
        try:
            self.debug = 1
            o_auth = tweepy.OAuthHandler('*******', '*******')
            o_auth .set_access_token('*******', '*******')
            self.twitter_api = tweepy.API( o_auth, parser=tweepy.parsers.JSONParser()  )
        except Exception:
            import traceback
            print traceback.format_exc()

    def searchTweetsVia(self, query):
        try:
            tweets = {}
            tweets = self.twitter_api.search( query, result_type='popular' )
            return ""
        except Exception:
            import traceback
            print traceback.format_exc()


if __name__ == '__main__':
    try:
        fs = cgi.FieldStorage()
        print "\n"
        query = fs.getvalue("query")
        SearchTweets().searchTweetsVia(query)
        print json.dumps(query)
    except Exception:
        import traceback
        print traceback.format_exc()

如何解决此错误。我需要 tweepy 库。

0 个答案:

没有答案