First off, I suggest that you always read everything from the HTTPResponse
object before calling conn.close()
. Even 404 responses contain a document.
I'm rather confused by your tracebacks, as far as I can see the http.client.BadStatusLine
should have been hidden by your except Exception
.
Typically an except Exception
clause isn't a good idea as unless you re-raise the same exception (you are not) you may be masking underlying problems. In any case, it's the first thing that should go when the code isn't working as expected.
Additionally the output you've provided doesn't seem to match the code you've provided.
Specifically, according to the traceback:
Connection Closed
Traceback (most recent call last):
File "httpTest.py", line 9, in http_get
resp = conn.getresponse()
The code for this has a print ('Connecting to ' + url)
immediately before:
print ('Connecting to ' + url)
conn.request(url, path, headers=headers)
resp = conn.getresponse()
But the line preceding the traceback in the output is Connection Closed
.
Update
Ignoring the confusing execution order of the try / finally
.
http.client.BadStatusLine
is raise when the initial response is not something like HTTP/1.1 200 OK
. In this particular case, it is <html>
instead.
Either the server is returning a documentwithout an HTTP header. Or it's an unexpected behaviour from the code.
I repeat what I've already said: always read everything from the HTTPResponse
object.
A packet capture will confirm what's going over the wire with this server.