Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes | Static Public Attributes | Private Member Functions | Private Attributes
socketserver.BaseServer Class Reference
Inheritance diagram for socketserver.BaseServer:
Inheritance graph
[legend]

List of all members.

Public Member Functions

def __init__
def server_activate
def serve_forever
def shutdown
def handle_request
def handle_timeout
def verify_request
def process_request
def server_close
def finish_request
def shutdown_request
def close_request
def handle_error

Public Attributes

 server_address
 RequestHandlerClass

Static Public Attributes

 timeout = None

Private Member Functions

def _handle_request_noblock

Private Attributes

 __is_shut_down
 __shutdown_request

Detailed Description

Base class for server classes.

Methods for the caller:

- __init__(server_address, RequestHandlerClass)
- serve_forever(poll_interval=0.5)
- shutdown()
- handle_request()  # if you do not use serve_forever()
- fileno() -> int   # for select()

Methods that may be overridden:

- server_bind()
- server_activate()
- get_request() -> request, client_address
- handle_timeout()
- verify_request(request, client_address)
- server_close()
- process_request(request, client_address)
- shutdown_request(request)
- close_request(request)
- handle_error()

Methods for derived classes:

- finish_request(request, client_address)

Class variables that may be overridden by derived classes or
instances:

- timeout
- address_family
- socket_type
- allow_reuse_address

Instance variables:

- RequestHandlerClass
- socket

Definition at line 150 of file socketserver.py.


Constructor & Destructor Documentation

def socketserver.BaseServer.__init__ (   self,
  server_address,
  RequestHandlerClass 
)
Constructor.  May be extended, do not override.

Reimplemented in idlelib.rpc.RPCServer, test.test_urllib2_localnet.LoopbackHttpServer, and test.test_wsgiref.MockServer.

Definition at line 196 of file socketserver.py.

00196 
00197     def __init__(self, server_address, RequestHandlerClass):
00198         """Constructor.  May be extended, do not override."""
00199         self.server_address = server_address
00200         self.RequestHandlerClass = RequestHandlerClass
00201         self.__is_shut_down = threading.Event()
00202         self.__shutdown_request = False

Here is the caller graph for this function:


Member Function Documentation

Handle one request, without blocking.

I assume that select.select has returned that the socket is
readable before this function was called, so there should be
no risk of blocking in get_request().

Definition at line 271 of file socketserver.py.

00271 
00272     def _handle_request_noblock(self):
00273         """Handle one request, without blocking.
00274 
00275         I assume that select.select has returned that the socket is
00276         readable before this function was called, so there should be
00277         no risk of blocking in get_request().
00278         """
00279         try:
00280             request, client_address = self.get_request()
00281         except socket.error:
00282             return
00283         if self.verify_request(request, client_address):
00284             try:
00285                 self.process_request(request, client_address)
00286             except:
00287                 self.handle_error(request, client_address)
00288                 self.shutdown_request(request)

Here is the call graph for this function:

Here is the caller graph for this function:

def socketserver.BaseServer.close_request (   self,
  request 
)
Called to clean up an individual request.

Reimplemented in socketserver.UDPServer, and socketserver.TCPServer.

Definition at line 329 of file socketserver.py.

00329 
00330     def close_request(self, request):
00331         """Called to clean up an individual request."""
00332         pass

Here is the caller graph for this function:

def socketserver.BaseServer.finish_request (   self,
  request,
  client_address 
)
Finish one request by instantiating RequestHandlerClass.

Definition at line 321 of file socketserver.py.

00321 
00322     def finish_request(self, request, client_address):
00323         """Finish one request by instantiating RequestHandlerClass."""
00324         self.RequestHandlerClass(request, client_address, self)

Here is the caller graph for this function:

def socketserver.BaseServer.handle_error (   self,
  request,
  client_address 
)
Handle an error gracefully.  May be overridden.

The default is to print a traceback and continue.

Reimplemented in idlelib.run.MyRPCServer, and idlelib.rpc.RPCServer.

Definition at line 333 of file socketserver.py.

00333 
00334     def handle_error(self, request, client_address):
00335         """Handle an error gracefully.  May be overridden.
00336 
00337         The default is to print a traceback and continue.
00338 
00339         """
00340         print('-'*40)
00341         print('Exception happened during processing of request from', end=' ')
00342         print(client_address)
00343         import traceback
00344         traceback.print_exc() # XXX But this goes to stderr!
00345         print('-'*40)
00346 

Here is the call graph for this function:

Here is the caller graph for this function:

Handle one request, possibly blocking.

Respects self.timeout.

Definition at line 253 of file socketserver.py.

00253 
00254     def handle_request(self):
00255         """Handle one request, possibly blocking.
00256 
00257         Respects self.timeout.
00258         """
00259         # Support people who used socket.settimeout() to escape
00260         # handle_request before self.timeout was available.
00261         timeout = self.socket.gettimeout()
00262         if timeout is None:
00263             timeout = self.timeout
00264         elif self.timeout is not None:
00265             timeout = min(timeout, self.timeout)
00266         fd_sets = select.select([self], [], [], timeout)
00267         if not fd_sets[0]:
00268             self.handle_timeout()
00269             return
00270         self._handle_request_noblock()

Here is the call graph for this function:

Here is the caller graph for this function:

Called if no new request arrives within self.timeout.

Overridden by ForkingMixIn.

Definition at line 289 of file socketserver.py.

00289 
00290     def handle_timeout(self):
00291         """Called if no new request arrives within self.timeout.
00292 
00293         Overridden by ForkingMixIn.
00294         """
00295         pass

Here is the caller graph for this function:

def socketserver.BaseServer.process_request (   self,
  request,
  client_address 
)
Call finish_request.

Overridden by ForkingMixIn and ThreadingMixIn.

Definition at line 304 of file socketserver.py.

00304 
00305     def process_request(self, request, client_address):
00306         """Call finish_request.
00307 
00308         Overridden by ForkingMixIn and ThreadingMixIn.
00309 
00310         """
00311         self.finish_request(request, client_address)
00312         self.shutdown_request(request)

Here is the call graph for this function:

Here is the caller graph for this function:

def socketserver.BaseServer.serve_forever (   self,
  poll_interval = 0.5 
)
Handle one request at a time until shutdown.

Polls for shutdown every poll_interval seconds. Ignores
self.timeout. If you need to do periodic tasks, do them in
another thread.

Definition at line 211 of file socketserver.py.

00211 
00212     def serve_forever(self, poll_interval=0.5):
00213         """Handle one request at a time until shutdown.
00214 
00215         Polls for shutdown every poll_interval seconds. Ignores
00216         self.timeout. If you need to do periodic tasks, do them in
00217         another thread.
00218         """
00219         self.__is_shut_down.clear()
00220         try:
00221             while not self.__shutdown_request:
00222                 # XXX: Consider using another file descriptor or
00223                 # connecting to the socket to wake this up instead of
00224                 # polling. Polling reduces our responsiveness to a
00225                 # shutdown request and wastes cpu at all other times.
00226                 r, w, e = select.select([self], [], [], poll_interval)
00227                 if self in r:
00228                     self._handle_request_noblock()
00229         finally:
00230             self.__shutdown_request = False
00231             self.__is_shut_down.set()

Here is the call graph for this function:

Called by constructor to activate the server.

May be overridden.

Reimplemented in socketserver.UDPServer, socketserver.TCPServer, and idlelib.rpc.RPCServer.

Definition at line 203 of file socketserver.py.

00203 
00204     def server_activate(self):
00205         """Called by constructor to activate the server.
00206 
00207         May be overridden.
00208 
00209         """
00210         pass

Called to clean-up the server.

May be overridden.

Reimplemented in socketserver.TCPServer.

Definition at line 313 of file socketserver.py.

00313 
00314     def server_close(self):
00315         """Called to clean-up the server.
00316 
00317         May be overridden.
00318 
00319         """
00320         pass

Here is the caller graph for this function:

Stops the serve_forever loop.

Blocks until the loop has finished. This must be called while
serve_forever() is running in another thread, or it will
deadlock.

Definition at line 232 of file socketserver.py.

00232 
00233     def shutdown(self):
00234         """Stops the serve_forever loop.
00235 
00236         Blocks until the loop has finished. This must be called while
00237         serve_forever() is running in another thread, or it will
00238         deadlock.
00239         """
00240         self.__shutdown_request = True
00241         self.__is_shut_down.wait()

def socketserver.BaseServer.shutdown_request (   self,
  request 
)
Called to shutdown and close an individual request.

Reimplemented in socketserver.UDPServer, and socketserver.TCPServer.

Definition at line 325 of file socketserver.py.

00325 
00326     def shutdown_request(self, request):
00327         """Called to shutdown and close an individual request."""
00328         self.close_request(request)

Here is the call graph for this function:

Here is the caller graph for this function:

def socketserver.BaseServer.verify_request (   self,
  request,
  client_address 
)
Verify the request.  May be overridden.

Return True if we should proceed with this request.

Definition at line 296 of file socketserver.py.

00296 
00297     def verify_request(self, request, client_address):
00298         """Verify the request.  May be overridden.
00299 
00300         Return True if we should proceed with this request.
00301 
00302         """
00303         return True

Here is the caller graph for this function:


Member Data Documentation

Definition at line 200 of file socketserver.py.

Definition at line 201 of file socketserver.py.

Definition at line 199 of file socketserver.py.

Reimplemented in socketserver.TCPServer.

Definition at line 198 of file socketserver.py.

Reimplemented in test.test_logging.LogRecordSocketReceiver.

Definition at line 194 of file socketserver.py.


The documentation for this class was generated from the following file: