if theurl.startswith(\"http://\"): theurl = theurl[7:]
head = theurl[:theurl.find(\'/\')]
tail = theurl[theurl.find(\'/\'):]
response_code = 0
import httplib
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.
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.
The Python Standard Library: httplib (Python 2) (called http.client in Python 3):
exception httplib.BadStatusLine
A subclass of HTTPException. Raised if a server responds with a HTTP status code that we don’t understand.
I saw this error when trying to access a HTTPS/SSL URL using httplib.HTTPConnection
You should use httplib.HTTPSConnection to access SSL urls.
From the documentation for httplib (Python 2) (called http.client in Python 3):
exception
httplib.
BadStatusLine
: (exceptionhttp.client.
BadStatusLine
:)A subclass of
HTTPException
.Raised if a server responds with an HTTP status code that we don’t understand.
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?
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.
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.
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.