
I'm trying to figure out the best way to catch all the different possible errors when using HTTPClientFactory and am kind of stuck. First, what is the best way to catch errors before or during the connection, such as connection time outs or dns lookup errors? If a response isn't sent to the client, status,version, and message will never get set. I need the status code for all responses, but I need a way to catch all errors that don't set the status so I know if status is available or not. I tried inheriting HTTPClientFactory and providing my own clientConnectionFailed() method, and that's when I ran into another issue. I happened to try connecting to a host that gave me an ssl handshake failure, and an exception was caught in twisted.internet.tcp and printed. However it didn't register an errback but went ahead with the callback (complete code is below). from twisted.internet import reactor,ssl from twisted.web.client import HTTPClientFactory import sys class Test: def Start(self): self.factory = HTTPClientFactory('https://www.he.net',method='GET') contextFactory = ssl.ClientContextFactory() reactor.connectSSL('www.he.net', 443, self.factory, contextFactory) self.factory.deferred.addCallback(self.Success) self.factory.deferred.addErrback(self.Error) reactor.run() def Success(self,data): print "Success: %s " % data reactor.stop() def Error(self,data): print "Error: %s " % data reactor.stop() c = Test() c.Start()

snacktime wrote:
I'm trying to figure out the best way to catch all the different possible errors when using HTTPClientFactory and am kind of stuck.
First, what is the best way to catch errors before or during the connection, such as connection time outs or dns lookup errors? If a response isn't sent to the client, status,version, and message will never get set. I need the status code for all responses, but I need a way to catch all errors that don't set the status so I know if status is available or not.
I tried inheriting HTTPClientFactory and providing my own clientConnectionFailed() method, and that's when I ran into another issue. I happened to try connecting to a host that gave me an ssl handshake failure, and an exception was caught in twisted.internet.tcp and printed. However it didn't register an errback but went ahead with the callback (complete code is below).
The error is server not supporting SSL3.0. HTTPClientFactory.connectionLost is called. HTTPPageGetter.connectionLost is called. http.HTTPClient.connectionLost is called. http.HTTPClient.handleResponseEnd is called. HTTPPageGetter.handleResponse is called with "". self.failed is 0 self.length is None HTTPClientFactory.page is called with "" HTTPPageGetter.transport.loseConnection is called. HTTPClientFactory.noPage is called. Hmmph, seems like that case should show up as an error and not a successful 0-length page. Please see these for more: http://twistedmatrix.com/bugs/issue883 http://twistedmatrix.com/bugs/issue882
participants (2)
-
snacktime
-
Tommi Virtanen