【问题标题】:Grooveshark closing connection early (httplib.BadStatusLine)Grooveshark 提前关闭连接 (httplib.BadStatusLine)
【发布时间】:2012-06-15 23:06:52
【问题描述】:

我正在尝试连接到grooveshark。因为这个 python 是我选择的语言。但我撞到了一堵砖墙。看起来grooveshark最近改变了他们协议的一部分,或者我可能遇到了python的限制。

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

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

从这项研究中,我开始查看grooveshark 的javascript 和flash 源代码,但这并没有返回任何有价值的东西。所以我做了任何理智的人花了 5 个小时查看反编译的动作脚本,而之前从未在其中编写过一行代码,并将其归咎于 Groovesharks 服务器。

具体来说,我认为grooveshark 可能会拒绝具有Connection: close 标头的连接。因此,我决定在 Chrome 的 REST Console 扩展程序中对其进行测试。

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

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

我在wireshark中测试过(在https中删除SSL,它确实发送Connection: keep-alive,这会导致grooveshark响应,但使用https required

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

完全改变了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 小时的会议,我敢打赌这已经在议程上。

更新:JackTheRipper51 已通知我,无论您发送什么内容,对grooveshark.com/more.php 的所有ssl 请求都会发生这种情况。这让我相信这是蟒蛇在捉弄我们。

更新 2:

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

我根本不需要 C。准备被激怒。一个简单的

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

box 给了我一个令牌...这里是 jsontest

{"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"}}

即使 json 在语法上不正确,它也总是返回 至少有一些标题!一直都是 Python...

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

【问题讨论】:

    标签: python httplib2 grooveshark


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 2010-09-13
      • 1970-01-01
      • 1970-01-01
      • 2021-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-27
      相关资源
      最近更新 更多