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

时间:2020-12-08 22:50:16
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

Does anyone know what "Bad Status Line" is?

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

Edit: I tried this for many servers, and many URL's and I still get this error?

编辑:我试过很多服务器,很多URL和我仍然得到这个错误?

10 个解决方案

#1


28  

From the documentation for httplib (Python 2) (called http.client in Python 3):

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

exception httplib.BadStatusLine: (exception http.client.BadStatusLine:)

异常httplib.BadStatusLine :(异常http.client.BadStatusLine :)

A subclass of HTTPException.

HTTPException的子类。

Raised if a server responds with an HTTP status code that we don’t understand.

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

I ran the same code and did not receive an error:

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

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

I guess just double-check everything and try again?

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

#2


24  

I recently got this error, in a situation where the method that contained the http request ran successfully once, and then threw this exception (with the status code as an empty string) the second time the method was called (with a different URL). I had a debugging advantage because this is calling my own REST api, so I did some logging on the server side and discovered that the request was never being received. I ultimately figured out that my URL string had a trailing newline character. So make sure that your URLs are stripped of any leading or trailing special characters.

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

#3


8  

The Python Standard Library: httplib (Python 2) (called http.client in Python 3):

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

exception httplib.BadStatusLine
A subclass of HTTPException. Raised if a server responds with a HTTP status code that we don’t understand.

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

#4


6  

Are you using a proxy?

您是否使用了代理?

If so, perhaps the proxy server is rejecting HEAD requests.

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

Do you get the same problem if you issue a GET request? If GET works I'd suspect that there is a proxy in your way.

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

You can see what's going on in more detail by calling conn.set_debuglevel(1) prior to calling conn.request(...).

您可以在调用conn.request(...)之前调用conn.set_debuglevel(1)来更详细地了解正在发生的事情。

#5


5  

I just found when we get exception httplib.BadStatusLine , is when server goes down and doesn't send any response, so it means web server doesn't even send the http code [1]

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

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

#6


3  

I've also encountered this problem. Accordig to GmailAPI (python), it happens when the server closes the connection before sending a valid respone. Indeed, this only happened to me when my program ran on large DB.

我也遇到过这个问题。根据GmailAPI(python),当服务器在发送有效的响应之前关闭连接时会发生这种情况。实际上,当我的程序在大型数据库上运行时,这只发生在我身上。

def _read_status(self):
    # Initialize with Simple-Response defaults
    line = self.fp.readline(_MAXLINE + 1)
    if len(line) > _MAXLINE:
        raise LineTooLong("header line")
    if self.debuglevel > 0:
        print "reply:", repr(line)
    if not line:
        # Presumably, the server closed the connection before
        # sending a valid response.
        raise BadStatusLine(line)

My solution was to move all the part that establishes the connection with gmail into a function. Then, call this function only before actually sending the email. Before that, the parts incharge of establishing the connection were just 'thrown' in some .py file, and therefore were called at the begining of the run.

我的解决方案是将与gmail建立连接的所有部分移动到一个函数中。然后,仅在实际发送电子邮件之前调用此函数。在此之前,建立连接的部分只是在某个.py文件中被“抛出”,因此在运行开始时被调用。

#7


2  

The problem I had was with multiple requests, but BadStatusLine was only occurring between requests with more then a 5 second interval with a Keep-Alive timeout=5. I'm still uncertain why BadStatusLine was being raised instead of NotConnected. It seems that the connection also defaults to 5 when the header is missing. The fix was conn.connect() before each request.

我遇到的问题是多个请求,但BadStatusLine仅发生在间隔超过5秒且保持活动超时= 5的请求之间。我仍然不确定为什么BadStatusLine被提升而不是NotConnected。当标头丢失时,似乎连接也默认为5。在每个请求之前修复了conn.connect()。

#8


2  

I know that "you should just use X" answers are frowned upon, but I have to say that after trying to diagnose this same problem for a couple hours I tried Requests with the same set up and it worked perfectly. Easier to use and debug in my opinion as well.

我知道“你应该只使用X”的答案是不受欢迎的,但我不得不说,在尝试诊断同样的问题几个小时之后,我尝试使用相同的设置进行请求,并且它工作得很好。在我看来也更容易使用和调试。

#9


1  

We have no clues about what is in your theurl string, and I do not know whether your problem is solved or not (6 years have past and I hope you made it long before), so I just give you one possible reason I met and share it with someone who may find this later.

我们没有关于你的theurl字符串中的内容的线索,我不知道你的问题是否已经解决(6年过去了,我希望你早就做到了),所以我只是给你一个可能的原因我遇到了与稍后可能会发现此内容的人分享。

I have encountered a quite similar problem, in which my code runs quite well on some computers but raises BadStatusLine exceptions sometimes. It is quite annoying just like a ghost.

我遇到了一个非常类似的问题,我的代码在某些计算机上运行得很好但有时会引发BadStatusLine异常。像幽灵一样令人讨厌。

After careful checked all the possible stituation, I found a 'Content-Length' component was in my request http header. After removeing the component, my code runs well in all computers. Maybe the first part of your theurl contains something like mine, which contradicts the true data length.

在仔细检查了所有可能的情况后,我发现在我的请求http标头中有一个'Content-Length'组件。删除组件后,我的代码在所有计算机上运行良好。也许你的theurl的第一部分包含类似我的东西,这与真正的数据长度相矛盾。

#10


0  

I saw this error when trying to access a HTTPS/SSL URL using httplib.HTTPConnection

尝试使用httplib.HTTPConnection访问HTTPS / SSL URL时,我看到了此错误

You should use httplib.HTTPSConnection to access SSL urls.

您应该使用httplib.HTTPSConnection来访问SSL URL。

#1


28  

From the documentation for httplib (Python 2) (called http.client in Python 3):

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

exception httplib.BadStatusLine: (exception http.client.BadStatusLine:)

异常httplib.BadStatusLine :(异常http.client.BadStatusLine :)

A subclass of HTTPException.

HTTPException的子类。

Raised if a server responds with an HTTP status code that we don’t understand.

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

I ran the same code and did not receive an error:

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

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

I guess just double-check everything and try again?

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

#2


24  

I recently got this error, in a situation where the method that contained the http request ran successfully once, and then threw this exception (with the status code as an empty string) the second time the method was called (with a different URL). I had a debugging advantage because this is calling my own REST api, so I did some logging on the server side and discovered that the request was never being received. I ultimately figured out that my URL string had a trailing newline character. So make sure that your URLs are stripped of any leading or trailing special characters.

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

#3


8  

The Python Standard Library: httplib (Python 2) (called http.client in Python 3):

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

exception httplib.BadStatusLine
A subclass of HTTPException. Raised if a server responds with a HTTP status code that we don’t understand.

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

#4


6  

Are you using a proxy?

您是否使用了代理?

If so, perhaps the proxy server is rejecting HEAD requests.

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

Do you get the same problem if you issue a GET request? If GET works I'd suspect that there is a proxy in your way.

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

You can see what's going on in more detail by calling conn.set_debuglevel(1) prior to calling conn.request(...).

您可以在调用conn.request(...)之前调用conn.set_debuglevel(1)来更详细地了解正在发生的事情。

#5


5  

I just found when we get exception httplib.BadStatusLine , is when server goes down and doesn't send any response, so it means web server doesn't even send the http code [1]

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

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

#6


3  

I've also encountered this problem. Accordig to GmailAPI (python), it happens when the server closes the connection before sending a valid respone. Indeed, this only happened to me when my program ran on large DB.

我也遇到过这个问题。根据GmailAPI(python),当服务器在发送有效的响应之前关闭连接时会发生这种情况。实际上,当我的程序在大型数据库上运行时,这只发生在我身上。

def _read_status(self):
    # Initialize with Simple-Response defaults
    line = self.fp.readline(_MAXLINE + 1)
    if len(line) > _MAXLINE:
        raise LineTooLong("header line")
    if self.debuglevel > 0:
        print "reply:", repr(line)
    if not line:
        # Presumably, the server closed the connection before
        # sending a valid response.
        raise BadStatusLine(line)

My solution was to move all the part that establishes the connection with gmail into a function. Then, call this function only before actually sending the email. Before that, the parts incharge of establishing the connection were just 'thrown' in some .py file, and therefore were called at the begining of the run.

我的解决方案是将与gmail建立连接的所有部分移动到一个函数中。然后,仅在实际发送电子邮件之前调用此函数。在此之前,建立连接的部分只是在某个.py文件中被“抛出”,因此在运行开始时被调用。

#7


2  

The problem I had was with multiple requests, but BadStatusLine was only occurring between requests with more then a 5 second interval with a Keep-Alive timeout=5. I'm still uncertain why BadStatusLine was being raised instead of NotConnected. It seems that the connection also defaults to 5 when the header is missing. The fix was conn.connect() before each request.

我遇到的问题是多个请求,但BadStatusLine仅发生在间隔超过5秒且保持活动超时= 5的请求之间。我仍然不确定为什么BadStatusLine被提升而不是NotConnected。当标头丢失时,似乎连接也默认为5。在每个请求之前修复了conn.connect()。

#8


2  

I know that "you should just use X" answers are frowned upon, but I have to say that after trying to diagnose this same problem for a couple hours I tried Requests with the same set up and it worked perfectly. Easier to use and debug in my opinion as well.

我知道“你应该只使用X”的答案是不受欢迎的,但我不得不说,在尝试诊断同样的问题几个小时之后,我尝试使用相同的设置进行请求,并且它工作得很好。在我看来也更容易使用和调试。

#9


1  

We have no clues about what is in your theurl string, and I do not know whether your problem is solved or not (6 years have past and I hope you made it long before), so I just give you one possible reason I met and share it with someone who may find this later.

我们没有关于你的theurl字符串中的内容的线索,我不知道你的问题是否已经解决(6年过去了,我希望你早就做到了),所以我只是给你一个可能的原因我遇到了与稍后可能会发现此内容的人分享。

I have encountered a quite similar problem, in which my code runs quite well on some computers but raises BadStatusLine exceptions sometimes. It is quite annoying just like a ghost.

我遇到了一个非常类似的问题,我的代码在某些计算机上运行得很好但有时会引发BadStatusLine异常。像幽灵一样令人讨厌。

After careful checked all the possible stituation, I found a 'Content-Length' component was in my request http header. After removeing the component, my code runs well in all computers. Maybe the first part of your theurl contains something like mine, which contradicts the true data length.

在仔细检查了所有可能的情况后,我发现在我的请求http标头中有一个'Content-Length'组件。删除组件后,我的代码在所有计算机上运行良好。也许你的theurl的第一部分包含类似我的东西,这与真正的数据长度相矛盾。

#10


0  

I saw this error when trying to access a HTTPS/SSL URL using httplib.HTTPConnection

尝试使用httplib.HTTPConnection访问HTTPS / SSL URL时,我看到了此错误

You should use httplib.HTTPSConnection to access SSL urls.

您应该使用httplib.HTTPSConnection来访问SSL URL。