Search code examples
pythontwitter

Reading an HTTP stream in python


I'm trying to read the output of the Twitter streaming API and I'm having some trouble. Here is my code so far:

from urllib2 import *
import StringIO

password_mgr = HTTPPasswordMgrWithDefaultRealm()
url = "https://stream.twitter.com/1/statuses/sample.json"
password_mgr.add_password(None, url, 'myusername', 'mypassword')
h = HTTPBasicAuthHandler(password_mgr)
opener = build_opener(h)
page = opener.open(url)
io = StringIO(page.read())
print io.getvalue()
io.close()

I initially was using just page.read() but caused my console to just keep printing out the stream and wouldn't return to the code. I'm now trying to use StringIO to process this like a stream and print it out incrementally, but when I do this I get nothing and the process just holds. Is there a better way to do this?


Solution

  • I hear good things about the requests module:

    https://2.python-requests.org/en/master/user/advanced/#id9