if theurl.startswith(\"http://\"): theurl = theurl[7:]
head = theurl[:theurl.find(\'/\')]
tail = theurl[theurl.find(\'/\'):]
response_code = 0
import httplib
Are you using a proxy?
If so, perhaps the proxy server is rejecting HEAD
requests.
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.
You can see what's going on in more detail by calling conn.set_debuglevel(1)
prior to calling conn.request(...)
.
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.
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.
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]
[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. 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.