Skip to content

Instantly share code, notes, and snippets.

@paulcnichols
Created September 24, 2012 21:48
Show Gist options
  • Save paulcnichols/3778634 to your computer and use it in GitHub Desktop.
Save paulcnichols/3778634 to your computer and use it in GitHub Desktop.
Sample python firehose client
import urllib2
import json
password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
password_mgr.add_password(None, 'https://stream.twitter.com/1/statuses/sample.json', '<user>', '<password>')
handler = urllib2.HTTPBasicAuthHandler(password_mgr)
opener = urllib2.build_opener(handler)
try:
conn = opener.open('https://stream.twitter.com/1/statuses/sample.json')
except urllib2.HTTPError, exception:
if exception.code == 401:
raise AuthenticationError("Access denied")
elif exception.code == 404:
raise ConnectionError("URL not found: %s" % self.url)
else: # re raise. No idea what would cause this, so want to know
raise
except urllib2.URLError, exception:
raise ConnectionError(exception.reason)
for line in conn:
try:
obj = json.loads(line)
print obj
except:
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment