[Web-SIG] httplib.HTTPConnection and Broken Pipe

Bart Whiteley bwhiteley at novell.com
Wed Apr 25 23:55:15 CEST 2007


I have an issue with the following code in
httplib.HTTPConnection.send(): 

        # send the data to the server. if we get a broken pipe, 
        # then close the socket. we want to reconnect when 
        # somebody tries to send again.
        #
        # NOTE: we DO propagate the error, though, because we 
        #     cannot simply ignore the error... the caller will 
        #     know if they can retry.
        if self.debuglevel > 0:
            print "send:", repr(str)
        try:
            self.sock.sendall(str)
        except socket.error, v:
            if v[0] == 32:      # Broken pipe
                self.close()
            raise

I'm sending a POST request, with a somewhat large (3K) request body.
The server requires authentication.  If the request doesn't have the
proper credentials, the server immediately sends a 401 response with an
authentication challenge, and closes the connection without reading the
request body.  The server does this to protect against DoS attacks.
When the client continues sending after the server closes the socket,
the server sends a TCP reset, and the client usually gets a socket error
of 104 (connection reset), but sometimes gets an error of 32 (broken
pipe) instead.  When I get a 104, I'm able to call getresponse(), and
read the 401 response.  Then I can retry the request with an appropriate
response to the authentication challenge.  When I get a socket error of
32, getresponse() throws an exception, so I don't have an authentication
challenge to respond to.  

If I remove the "try:... except:" code above, so that self.close() does
not get called on a broken pipe error, the http client code works
nicely.  In spite of the broken pipe, I can retrieve the response, and
can retry the request with an appropriate authentication header. 

Is anyone aware of a way to work around this problem? 



More information about the Web-SIG mailing list