python - Grooveshark 提前关闭连接 (httplib.BadStatusLine)

标签 python httplib2 grooveshark

我正在尝试连接到groovyshark。因为 python 是我选择的语言。但我已经碰壁了。看来groveshark最近改变了他们的协议(protocol)的一部分,或者我可能遇到了python的限制。

我正在与来自github的JackTheRipper51“一起”工作,他为groveshark制作了这个库:https://github.com/jacktheripper51/groove-dl 它实际上不是一个库,但我很快就重新编码为一个库。

本周早些时候,它运行良好,我能够将它用于我的项目。但两天前,它开始在 getToken 函数上失败,httplib 开始返回 httplib.BadStatusLine: '' 根据我的研究,这意味着服务器提前关闭了连接。

通过这项研究,我开始研究groveshark的javascript和flash源代码,但这并没有返回任何有值(value)的东西。所以我做了任何一个理智的人花了 5 个小时查看反编译的 ActionScript 而之前从未在其中编写过一行代码的人都会做的事情,并将其归咎于grovesharks服务器。

具体来说,我认为groveshark可能会拒绝具有Connection: close header 的连接。因此,我决定在 Chrome 的 REST Console 扩展中测试它。

我让 python 脚本转储它正在编码的 json,然后将其粘贴到 Rest 控制台中,点击 POST,它返回正常,并带有预期的数据。我现在确信我是对的并非不可能。

我的下一步是在 httplib2 中进行编码(因为它支持 Connection: keep-alive),但问题仍然存在。

我已经在wireshark中进行了测试(删除https中的SSL,它确实发送Connection: keep-alive,这会导致groveshark响应,但需要https)

我只修改了一小部分代码。

完全改变了 getToken()

def getToken():
    global staticHeader, _token
    post = {}
    post["parameters"] = {}
    post["parameters"]["secretKey"] = hashlib.md5(staticHeader["session"]).hexdigest()
    post["method"] = "getCommunicationToken"
    post["header"] = staticHeader
    post["header"]["client"] = "htmlshark"
    post["header"]["clientRevision"] = "20120312"
    header = {"User-Agent": _useragent, "Referer": _referer, "Content-Type":"application/json", "Cookie":"PHPSESSID=" + staticHeader["session"], "Connection":"keep-alive"}
    response, content = http.request("https://grooveshark.com/more.php?getCommunicationToken", "POST" ,body = json.JSONEncoder().encode(post), headers = header)
    print response
    #_token = json.JSONDecoder().decode(gzip.GzipFile(fileobj=(StringIO.StringIO(conn.getresponse().read()))).read())["result"]
    #print _token

我添加了 httplib2 初始化的内容:

http = httplib2.Http()

我导入了httplib2:

import httplib, httplib2

我还重命名了 json 构造函数,只是因为我想要更具描述性。

完整的回溯是:

Traceback (most recent call last):
  File "C:\Users\Delusional Logic\Documents\GitHub\groove-dl\python\groove.py", line 141, in <module>
    getToken()
  File "C:\Users\Delusional Logic\Documents\GitHub\groove-dl\python\groove.py", line 51, in getToken
    response, content = http.request("https://grooveshark.com/more.php?getCommunicationToken", "POST" ,body = json.JSONEncoder().encode(post), headers = header)
  File "C:\Python27\lib\site-packages\httplib2-0.7.4-py2.7.egg\httplib2\__init__.py", line 1544, in request
    (response, content) = self._request(conn, authority, uri, request_uri, method, body, headers, redirections, cachekey)
  File "C:\Python27\lib\site-packages\httplib2-0.7.4-py2.7.egg\httplib2\__init__.py", line 1294, in _request
    (response, content) = self._conn_request(conn, request_uri, method, body, headers)
  File "C:\Python27\lib\site-packages\httplib2-0.7.4-py2.7.egg\httplib2\__init__.py", line 1264, in _conn_request
    response = conn.getresponse()
  File "C:\Python27\lib\httplib.py", line 1027, in getresponse
    response.begin()
  File "C:\Python27\lib\httplib.py", line 407, in begin
    version, status, reason = self._read_status()
  File "C:\Python27\lib\httplib.py", line 371, in _read_status
    raise BadStatusLine(line)
httplib.BadStatusLine: ''

是什么导致了 BadStatusLine,以及如何修复它。

PS 我知道事实上他们在此事爆发前一天举行了一次长达 8 小时的 session ,我敢打赌这已列入议程。

更新:JackTheRipper51 告诉我,无论您发送什么内容,向grooveshark.com/more.php 发出的所有 ssl 请求都会发生这种情况。这让我相信这是Python在捉弄我们。

更新2:

JackTheRipper51 刚刚告诉我它确实是 python。这是他的帖子:

I didn't need C at all. Prepare to be outraged. A simple

curl -H "Content-Type: text/plain" -d "@jsontest" "https://grooveshark.com/more.php?getCommunicationToken" -v on a linux

box got me a token... jsontest here being

{"header":{"client":"mobileshark","clientRevision":"20120227","privacy":0,"country":{"ID":63,"CC1":4611686018427388000,"CC2":0,"CC3":0,"CC4":0,"DMA":0,"IPR":0},"uuid":"BF5D03EE-91BB-40C9-BE7B-11FD43CAF0F0","session":"1d9989644c5eba85958d675b421fb0ac"},"method":"getCommunicationToken","parameters":{"secretKey":"230147db390cf31fc3b8008e85f8a7f1"}}

Even when the json is not syntactically correct, it always returns at least some headers! It's been Python all along...

剩下的唯一问题是为什么 python 这样做?

最佳答案

问题已“解决”,或已找到原因。

JackTheRipper 将错误提交给 python,他们确认这确实是 ssl 的问题,更具体地说是 openssl 0.9.8,导致连接超时。

错误报告: http://bugs.python.org/issue15082

关于python - Grooveshark 提前关闭连接 (httplib.BadStatusLine),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11059229/

相关文章:

Python:BASIC 中是否有对应的 mid、right 和 left?

python - [python3.6] 的 Httplib2 无效语法错误

Grooveshark 代码

jquery - Grooveshark 公共(public) API - 未找到方法

python - 如何在 python 中实现 Unicode 缓冲区

python - 如何在 pandas 数据框中使用 bool 索引来表示子字符串关系?

javascript - 从tinysong获取歌曲

Python 3 - 类型错误 : 'map' object is not subscriptable

python - 尝试向 couchdb 发出请求时,HttpLib2 抛出错误