The Requests streaming example does not work in my environment

你说的曾经没有我的故事 提交于 2019-12-03 08:10:14

You need to switch off prefetching, which I think is a parameter that changed defaults:

r = requests.post('https://stream.twitter.com/1/statuses/filter.json',
    data={'track': 'requests'}, auth=('username', 'password'),
    prefetch=False)

for line in r.iter_lines():
    if line: # filter out keep-alive new lines
        print json.loads(line)

UPDATE: In the latest requests framework, use stream instead of prefetch:

r = requests.post('https://stream.twitter.com/1/statuses/filter.json',
    data={'track': 'requests'}, auth=('username', 'password'),
    stream=True)

for line in r.iter_lines():
    if line: # filter out keep-alive new lines
        print json.loads(line)

Ah, I found the answer by reading the code. At some point, a prefetch parameter was added to the post method (and other methods, I assume).

I just needed to add a prefetch=False kwarg to requests.post().

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!