Mike Flynn
Mike Flynn

Reputation: 1027

requests.get keeps hanging with Twitter Api (Python Requests library)

My code so far:

import requests
import json

url = "https://stream.twitter.com/1/statuses/sample.json"

r = requests.get(url, auth = ('username', 'password'))
print r.status_code

When I run this script in the console it keeps hanging, but when I run the equivalent urllib2 I get a good response:

from urllib2 import *

password_mgr = HTTPPasswordMgrWithDefaultRealm()
url = "https://stream.twitter.com/1/statuses/sample.json"
password_mgr.add_password(None, url, 'username', 'password')
h = HTTPBasicAuthHandler(password_mgr)
opener = build_opener(h)
page = opener.open(url)
print page.getcode()

This returns 200. Anyone have any idea what the problem is?

Edit: Also, when I adjust the password in the above code, I get the appropriate 401 reponse. I think the reason for this is that there is some blocking going on?

Upvotes: 2

Views: 1436

Answers (2)

drewrobb
drewrobb

Reputation: 1604

I had this problem as well, it only affects requests versions > 0.13.5. To fix, disable prefetch in the requests.get call:

r = requests.get(url, auth = ('username', 'password'), prefetch=False)

Upvotes: 1

codegeek
codegeek

Reputation: 33289

Not sure if you can call requests.get the way you are doing. If parameters are passed, you need to include a dictionary. Something like this ?

auth = {'username': 'username', 'password': 'password'}
r = requests.get("https://stream.twitter.com/1/statuses/sample.json", params=auth)
print r.status_code

Read this link overall for the correct way to use requests.get http://docs.python-requests.org/en/latest/user/quickstart/

Upvotes: 0

Related Questions