SocketServer oversite?

scott cotton scott at chronis.pobox.com
Mon May 31 01:46:37 EDT 1999


On Sun, May 30, 1999 at 05:48:21PM +0000, Laurence Tratt wrote:
| In SocketServer in the standard library, the method "handle_request" calls
| "get_request" thus:
| 
|     def handle_request(self):
|         """Handle one request, possibly blocking."""
|         request, client_address = self.get_request()
|         if self.verify_request(request, client_address):
|             try:
|                 self.process_request(request, client_address)
|             except:
|                 self.handle_error(request, client_address)
| 
| and get_request is thus defined:
| 
|     def get_request(self):
|         """Get the request and client address from the socket.
| 
|         May be overridden.
| 
|         """
|         return self.socket.accept()
| 
| The problem with this is that socket.accept can throw an error: this then
| causes a cascading error throughout the SocketServer, as it's not caught at
| any point. In fact, it caused an http server I am involved with to die
| completely with some web browsers/platforms/users :)
| 
| It would seem to make sense for either handle_request or get_request to do a
| try: on this, in order that it won't kill the server. I suspect that the
| easiest solution (although not necessarily the best) would be to redo
| handle_request along the lines of:
| 
|     def handle_request(self):
|         """Handle one request, possibly blocking."""
|         try:
|             request, client_address = self.get_request()
|             if self.verify_request(request, client_address):
|                 self.process_request(request, client_address)
|         except:
|             self.handle_error(request, client_address)
| 
| Of course, I could well be missing the point completely here :)

I think that's a good idea.  it's important to check the
return values of system calls in C, and so it's important to
check their equivalents in python.  the only change i would
add to your code is make it check explicitly for
'socket.errer', sinde that's what arises when accept breaks.

scott




More information about the Python-list mailing list