Why am I getting this error in python ? (httplib)

后端 未结 10 1617
情话喂你
情话喂你 2020-11-27 20:11
if theurl.startswith(\"http://\"): theurl = theurl[7:]
    head = theurl[:theurl.find(\'/\')]
    tail = theurl[theurl.find(\'/\'):]
response_code = 0
import httplib         


        
相关标签:
10条回答
  • 2020-11-27 20:52

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

    0 讨论(0)
  • 2020-11-27 20:54

    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.

    0 讨论(0)
  • 2020-11-27 20:55

    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

    0 讨论(0)
  • 2020-11-27 20:55

    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.

    0 讨论(0)
提交回复
热议问题