为什么我在python中收到此错误?(httplib的)

TIM*_*MEX 26 python http

if theurl.startswith("http://"): theurl = theurl[7:]
    head = theurl[:theurl.find('/')]
    tail = theurl[theurl.find('/'):]
response_code = 0
import httplib
conn = httplib.HTTPConnection(head)
conn.request("HEAD",tail)
res = conn.getresponse()
response_code = int(res.status)

http://www.garageband.com/mp3cat/.UZCKbS6N4qk/01_Saraenglish.mp3
Traceback (most recent call last):
  File "check_data_404.py", line 51, in <module>
    run()
  File "check_data_404.py", line 35, in run
    res = conn.getresponse()
  File "/usr/lib/python2.6/httplib.py", line 950, in getresponse
    response.begin()
  File "/usr/lib/python2.6/httplib.py", line 390, in begin
    version, status, reason = self._read_status()
  File "/usr/lib/python2.6/httplib.py", line 354, in _read_status
    raise BadStatusLine(line)
httplib.BadStatusLine
Run Code Online (Sandbox Code Playgroud)

有谁知道"坏状态线"是什么?

编辑:我试过很多服务器,很多网址和我仍然会收到此错误?

jat*_*ism 30

httplib(Python 2)的文档(在Python 3中称为http.client):

例外 httplib.BadStatusLine :( 例外 http.client.BadStatusLine :)

的子类HTTPException.

如果服务器使用我们不理解的HTTP状态代码进行响应,则引发此异常.

我运行相同的代码,但没有收到错误:

>>> theurl = 'http://www.garageband.com/mp3cat/.UZCKbS6N4qk/01_Saraenglish.mp3'
>>> if theurl.startswith("http://"):
...     theurl = theurl[7:]
...     head = theurl[:theurl.find('/')]
...     tail = theurl[theurl.find('/'):]
... 
>>> head
'www.garageband.com'
>>> tail
'/mp3cat/.UZCKbS6N4qk/01_Saraenglish.mp3'
>>> response_code = 0
>>> import httplib
>>> conn = httplib.HTTPConnection(head)
>>> conn.request("HEAD", tail)
>>> res = conn.getresponse()
>>> res.status
302
>>> response_code = int(res.status)
Run Code Online (Sandbox Code Playgroud)

我想只需仔细检查一切,然后再试一次?


aaa*_*rgh 25

我最近收到此错误,在包含http请求的方法成功运行一次,然后第二次调用该方法(使用不同的URL)时抛出此异常(状态代码为空字符串)的情况下.我有调试优势,因为这是调用我自己的REST api,所以我在服务器端进行了一些日志记录,发现请求从未被收到.我最终发现我的URL字符串有一个尾随的换行符.因此,请确保您的网址已被删除任何前导或尾随特殊字符.

  • 太棒了......这为我修好了...绝对不会怀疑换行会出现这样一个无关的错误信息.我很幸运,在这个职位发布四年后,阿拉和我有同样的问题. (5认同)

art*_*nil 9

Python标准库:httplib(Python 2)(在Python 3中称为http.client):

exception httplib.BadStatusLine
HTTPException的子类.如果服务器使用我们不理解的HTTP状态代码进行响应,则引发此异常.


mha*_*wke 7

您是否使用了代理?

如果是这样,代理服务器可能拒绝HEAD请求.

如果您发出GET请求,是否会遇到同样的问题?如果GET工作,我怀疑你的方式有代理.

您可以在致电conn.set_debuglevel(1)之前致电查看更详细的信息conn.request(...).


Sér*_*gio 5

我刚刚发现当我们得到异常httplib.BadStatusLine时,是服务器发生故障并且没有发送任何响应,所以这意味着Web服务器甚至不发送http代码[1]

[1] http://en.wikipedia.org/wiki/List_of_HTTP_status_codes