'str'对象没有属性'disconnect'

时间:2013-09-30 02:30:45

标签: python django twython

这种方法有什么问题? 'stream'是我声明的方法名称,用于启动流转化器并且它有效。但是,当我写这个方法来断开流媒体时,它有错误说'str'对象没有属性'disconnect'。谁能举个例子或告诉我我错过了什么?

def twitter_stop(request):
    stream = request.session['stream']

    stream.disconnect()
     return HttpResponse(request)

更新: 这是我开始使用的方法:

def twitter_start(request):
     stream = MyStreamer(settings.CONSUMER_KEY, settings.CONSUMER_SECRET,
                settings.ACCESS_TOKEN_KEY, settings.ACCESS_TOKEN_SECRET)
     # get the stream object from the SESSION
      request.session['stream'] = stream
     # get the keywords from the models
        keywords = Keys.objects.all()
        stream.statuses.filter(track=keywords)
         return HttpResponse(request)

1 个答案:

答案 0 :(得分:-1)

2个请求对象不同;您在 twitter_start 中指定的流 执行该功能后, request.session 会丢失。使用全局变量 存储流,例如:

global STREAM

def twitter_start(request):
    global STREAM
    STREAM = MyStreamer(settings.CONSUMER_KEY, settings.CONSUMER_SECRET,
                        settings.ACCESS_TOKEN_KEY, settings.ACCESS_TOKEN_SECRET)

    # get the keywords from the models
    keywords = Keys.objects.all()
    STREAM.statuses.filter(track=keywords)
    return HttpResponse(request)

def twitter_stop(request):
    STREAM.disconnect()
    return HttpResponse(request)