2.1 and SocketServer.py

Michael Ströder michael at stroeder.com
Sun Apr 15 20:12:19 EDT 2001


HI!

I derived my own HTTP server from SocketServer.TCPServer and defined
a multi-threaded variant in an own class (mainly for own exception
logging, see code below).

This used to work in Python 2.0 but not in Python 2.1. I understand
that there's a new base class SocketServer.BaseServer but I couldn't
figure out how that affects my stuff. After the main thread is
stopped I get the following exception:

Traceback (most recent call last):
  File
"/home/michael/Proj/python/ldap/web2ldap/pylib/msHTTPServer.py",
line 79, in run
    apply(self._func,(self._request,self._client_address))
  File "/usr/lib/python2.1/SocketServer.py", line 246, in
finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "/usr/lib/python2.1/SocketServer.py", line 494, in __init__
    self.setup()
  File "/usr/lib/python2.1/SocketServer.py", line 537, in setup
    self.rfile = self.connection.makefile('rb', self.rbufsize)
error: (9, 'Ung\xfcltiger Dateideskriptor')

Any clue?

Ciao, Michael.

------------------------------------------------------------------------

class MyHTTPServer(SocketServer.TCPServer):
  
  def __init__(self, server_address, RequestHandlerClass):
    self.access_log = sys.stdout
    self.error_log = sys.stderr
    SocketServer.TCPServer.__init__(self, server_address,
RequestHandlerClass)

  def server_bind(self):
    """Override server_bind to set socket options."""
    self.socket.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
    SocketServer.TCPServer.server_bind(self)

  def handle_error(self,request,client_address):
      """
      Generic low-level handler for exceptions
      """
      import traceback
      exc_obj,exc_value,exc_traceback = sys.exc_info()
      print 'Time: %s\nClient address: %s:%d' % (
        time.strftime(
          '%Y-%m-%dT%H:%M:%SZ',time.gmtime(time.time())
        ),
        client_address[0],client_address[1],
      )
      if isinstance(exc_value,KeyboardInterrupt):
        raise KeyboardInterrupt
      elif isinstance(exc_value,IOError) and exc_value.errno==32:
        print 'IOError %s: User interrupted connection.' % (
          repr(exc_value.errno)
        )
      else:
        traceback.print_exception(exc_obj,exc_value,exc_traceback)
      print '-'*66

try:
  import threading
except ImportError:
  MyThreadingHTTPServer = MyHTTPServer
else:

  class MyHTTPThread(threading.Thread):
    """Thread class for HTTP handler thread"""
    def __init__(self,func,request,client_address,handle_error):
      self._func=func
      self._request=request
      self._client_address=client_address
      self._handle_error=handle_error
      threading.Thread.__init__(self)
    def run(self):
      try:
        apply(self._func,(self._request,self._client_address))
      except:
       
apply(self._handle_error,(self._request,self._client_address))

  class MyThreadingHTTPServer(MyHTTPServer):
    def process_request(self, request, client_address):
      """Start a new thread to process the request."""
      t = MyHTTPThread(
        self.finish_request,request,client_address,self.handle_error
      )
      t.start()



More information about the Python-list mailing list