python - requests.get 一直卡在 Twitter Api(Python 请求库)上

标签 python json twitter python-requests

到目前为止我的代码:

import requests
import json

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

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

当我在控制台中运行这个脚本时,它一直挂起,但是当我运行等效的 urllib2 时,我得到了很好的响应:

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()

这将返回 200。有人知道问题出在哪里吗?

编辑:另外,当我在上面的代码中调整密码时,我得到了相应的 401 响应。我认为这是因为发生了一些阻塞?

最佳答案

我也有这个问题,它只影响请求版本 > 0.13.5。 要修复,请在 requests.get 调用中禁用预取:

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

关于python - requests.get 一直卡在 Twitter Api(Python 请求库)上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11903231/

相关文章:

python - 使用函数调用打印字符串

php - 重新排列 MySQLi 查询的数组结果

Python - 如何在用户的时间线中搜索?

sql - 是否有可能在 YQL 中获得两个结果集的交集或差异?

namespaces - 解析twitter API命名空间georss :point Not bound

python - 在 django 模板引擎中访问字典

python - 检查时间戳列是否在另一个数据帧的日期范围内

python - 使用正确的标签 python 绘制图例

PHP - json 不通过循环解析

java - org.json.JSONException : Value Data of type java. lang.String 无法转换为 JSONObject