简体   繁体   English

为什么我在 python 中收到 httplib.BadStatusLine?

[英]Why am I getting httplib.BadStatusLine in python?

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 都试过了,但我仍然收到这个错误?

From the documentation for httplib (Python 2) (called http.client in Python 3 ): httplib(Python 2)的文档 在Python 3中称为http.client ):

exception httplib. 异常 httplib. BadStatusLine : ( exception http.client. BadStatusLine :) 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? 我想只需仔细检查一切,然后再试一次?

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). 我最近收到此错误,在包含http请求的方法成功运行一次,然后第二次调用该方法(使用不同的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. 我有调试优势,因为这是调用我自己的REST api,所以我在服务器端进行了一些日志记录,发现请求从未被收到。 I ultimately figured out that my URL string had a trailing newline character. 我最终发现我的URL字符串有一个尾随的换行符。 So make sure that your URLs are stripped of any leading or trailing special characters. 因此,请确保您的网址已被删除任何前导或尾随特殊字符。

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. HTTPException的子类。 Raised if a server responds with a HTTP status code that we don't understand. 如果服务器使用我们不理解的HTTP状态代码进行响应,则引发此异常。

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? 如果您发出GET请求,是否会遇到同样的问题? If GET works I'd suspect that there is a proxy in your way. 如果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)来更详细地了解正在发生的事情。

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 [1] http://en.wikipedia.org/wiki/List_of_HTTP_status_codes

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. 我遇到的问题是多个请求,但BadStatusLine仅发生在间隔超过5秒且保持活动超时= 5的请求之间。 I'm still uncertain why BadStatusLine was being raised instead of NotConnected. 我仍然不确定为什么BadStatusLine被提升而不是NotConnected。 It seems that the connection also defaults to 5 when the header is missing. 当标头丢失时,似乎连接也默认为5。 The fix was conn.connect() before each request. 在每个请求之前修复了conn.connect()

I've also encountered this problem. 我也遇到过这个问题。 Accordig to GmailAPI (python), it happens when the server closes the connection before sending a valid respone. 根据GmailAPI(python),当服务器在发送有效的响应之前关闭连接时会发生这种情况。 Indeed, this only happened to me when my program ran on large DB. 实际上,当我的程序在大型数据库上运行时,这只发生在我身上。

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. 我的解决方案是将与gmail建立连接的所有部分移动到一个函数中。 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. 在此之前,建立连接的部分只是在某个.py文件中被“抛出”,因此在运行开始时被调用。

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. 我知道“你应该只使用X”的答案是不受欢迎的,但我不得不说,在尝试诊断同样的问题几个小时后,我尝试使用相同的设置进行请求 ,并且它工作得很好。 Easier to use and debug in my opinion as well. 在我看来也更容易使用和调试。

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. 我遇到了一个非常类似的问题,我的代码在某些计算机上运行得很好但有时会引发BadStatusLine异常。 It is quite annoying just like a ghost. 像鬼一样令人讨厌。

After careful checked all the possible stituation, I found a 'Content-Length' component was in my request http header . 仔细检查了所有可能的情况后,我发现在我的请求http header有一个'Content-Length'组件。 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. 也许你的theurl的第一部分包含类似我的东西,这与真正的数据长度相矛盾。

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。

I ran into this problem with a python tool I was supporting.我使用支持的 python 工具遇到了这个问题。 It turns out that we had the wrong port info for a service.事实证明我们有错误的服务端口信息。 We were connecting to a server just fine, just not the one we thought we were.我们连接到一台服务器很好,只是不是我们认为的那样。 It turns out some not-HTTP server was running on that port and returned its own sort of error message.结果是一些非 HTTP 服务器在该端口上运行并返回了它自己的错误消息。 The requests stuff couldn't translate the not-HTTP response into an HTTPResponse, but the first problem it runs into is looking at the first line of the non-HTTP response.请求无法将非 HTTP 响应转换为 HTTPResponse,但它遇到的第一个问题是查看非 HTTP 响应的第一行。

Curiously, we were trying the same thing in curl with no problem, but then someone pointed out that we had typed out the right port by habit instead of the port we had been given and had typed into the code.奇怪的是,我们在 curl 中尝试同样的事情没有问题,但后来有人指出我们已经按照习惯输入了正确的端口,而不是我们已经输入并输入代码的端口。 It took much longer than we'd like to admit that we can't tell the difference between two numbers.花费的时间比我们愿意承认的要长得多,因为我们无法分辨两个数字之间的区别。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM