<!DOCTYPE html PUBLIC
"-//W3C//DTD HTML 4.0 Transitional//EN">
<link rel=
"STYLESHEET" href=
"lib.css" type='text/css'
/>
<link rel=
"SHORTCUT ICON" href=
"../icons/pyfav.png" type=
"image/png" />
<link rel='start' href='../index.html' title='Python Documentation Index'
/>
<link rel=
"first" href=
"lib.html" title='Python Library Reference'
/>
<link rel='contents' href='contents.html'
title=
"Contents" />
<link rel='index' href='genindex.html' title='Index'
/>
<link rel='last' href='about.html' title='About this document...'
/>
<link rel='help' href='about.html' title='About this document...'
/>
<link rel=
"next" href=
"node537.html" />
<link rel=
"prev" href=
"node535.html" />
<link rel=
"parent" href=
"module-SocketServer.html" />
<link rel=
"next" href=
"node537.html" />
<meta name='aesop' content='information'
/>
<title>11.16.2 Server Objects
</title>
<div id='top-navigation-panel' xml:id='top-navigation-panel'
>
<table align=
"center" width=
"100%" cellpadding=
"0" cellspacing=
"2">
<td class='online-navigation'
><a rel=
"prev" title=
"11.16.1 Server Creation Notes"
href=
"node535.html"><img src='../icons/previous.png'
border='
0' height='
32' alt='Previous Page' width='
32'
/></A></td>
<td class='online-navigation'
><a rel=
"parent" title=
"11.16 SocketServer "
href=
"module-SocketServer.html"><img src='../icons/up.png'
border='
0' height='
32' alt='Up One Level' width='
32'
/></A></td>
<td class='online-navigation'
><a rel=
"next" title=
"11.16.3 RequestHandler Objects"
href=
"node537.html"><img src='../icons/next.png'
border='
0' height='
32' alt='Next Page' width='
32'
/></A></td>
<td align=
"center" width=
"100%">Python Library Reference
</td>
<td class='online-navigation'
><a rel=
"contents" title=
"Table of Contents"
href=
"contents.html"><img src='../icons/contents.png'
border='
0' height='
32' alt='Contents' width='
32'
/></A></td>
<td class='online-navigation'
><a href=
"modindex.html" title=
"Module Index"><img src='../icons/modules.png'
border='
0' height='
32' alt='Module Index' width='
32'
/></a></td>
<td class='online-navigation'
><a rel=
"index" title=
"Index"
href=
"genindex.html"><img src='../icons/index.png'
border='
0' height='
32' alt='Index' width='
32'
/></A></td>
<div class='online-navigation'
>
<b class=
"navlabel">Previous:
</b>
<a class=
"sectref" rel=
"prev" href=
"node535.html">11.16.1 Server Creation Notes
</A>
<b class=
"navlabel">Up:
</b>
<a class=
"sectref" rel=
"parent" href=
"module-SocketServer.html">11.16 SocketServer
</A>
<b class=
"navlabel">Next:
</b>
<a class=
"sectref" rel=
"next" href=
"node537.html">11.16.3 RequestHandler Objects
</A>
<!--End of Navigation Panel-->
<H2><A NAME=
"SECTION00131620000000000000000">
11.16.2 Server Objects
</A>
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
3548' xml:id='l2h-
3548'
class=
"function">fileno
</tt></b>(
</nobr></td>
<td><var></var>)
</td></tr></table></dt>
Return an integer file descriptor for the socket on which the server
is listening. This function is most commonly passed to
<tt class=
"function">select.select()
</tt>, to allow monitoring multiple servers in the
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
3549' xml:id='l2h-
3549'
class=
"function">handle_request
</tt></b>(
</nobr></td>
<td><var></var>)
</td></tr></table></dt>
Process a single request. This function calls the following methods
in order:
<tt class=
"method">get_request()
</tt>,
<tt class=
"method">verify_request()
</tt>, and
<tt class=
"method">process_request()
</tt>. If the user-provided
<tt class=
"method">handle()
</tt>
method of the handler class raises an exception, the server's
<tt class=
"method">handle_error()
</tt> method will be called.
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
3550' xml:id='l2h-
3550'
class=
"function">serve_forever
</tt></b>(
</nobr></td>
<td><var></var>)
</td></tr></table></dt>
Handle an infinite number of requests. This simply calls
<tt class=
"method">handle_request()
</tt> inside an infinite loop.
<dl><dt><b><tt id='l2h-
3551' xml:id='l2h-
3551'
>address_family
</tt></b></dt>
The family of protocols to which the server's socket belongs.
<tt class=
"constant">socket.AF_INET
</tt> and
<tt class=
"constant">socket.AF_UNIX
</tt> are two
<dl><dt><b><tt id='l2h-
3552' xml:id='l2h-
3552'
>RequestHandlerClass
</tt></b></dt>
The user-provided request handler class; an instance of this class is
created for each request.
<dl><dt><b><tt id='l2h-
3553' xml:id='l2h-
3553'
>server_address
</tt></b></dt>
The address on which the server is listening. The format of addresses
varies depending on the protocol family; see the documentation for the
socket module for details. For Internet protocols, this is a tuple
containing a string giving the address, and an integer port number:
<code>('
127.0.0.1',
80)
</code>, for example.
<dl><dt><b><tt id='l2h-
3554' xml:id='l2h-
3554'
>socket
</tt></b></dt>
The socket object on which the server will listen for incoming requests.
The server classes support the following class variables:
<dl><dt><b><tt id='l2h-
3555' xml:id='l2h-
3555'
>allow_reuse_address
</tt></b></dt>
Whether the server will allow the reuse of an address. This defaults
to
<tt class=
"constant">False
</tt>, and can be set in subclasses to change the policy.
<dl><dt><b><tt id='l2h-
3556' xml:id='l2h-
3556'
>request_queue_size
</tt></b></dt>
The size of the request queue. If it takes a long time to process a
single request, any requests that arrive while the server is busy are
placed into a queue, up to
<tt class=
"member">request_queue_size
</tt> requests. Once
the queue is full, further requests from clients will get a
``Connection denied'' error. The default value is usually
5, but this
can be overridden by subclasses.
<dl><dt><b><tt id='l2h-
3557' xml:id='l2h-
3557'
>socket_type
</tt></b></dt>
The type of socket used by the server;
<tt class=
"constant">socket.SOCK_STREAM
</tt>
and
<tt class=
"constant">socket.SOCK_DGRAM
</tt> are two possible values.
There are various server methods that can be overridden by subclasses
of base server classes like
<tt class=
"class">TCPServer
</tt>; these methods aren't
useful to external users of the server object.
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
3558' xml:id='l2h-
3558'
class=
"function">finish_request
</tt></b>(
</nobr></td>
<td><var></var>)
</td></tr></table></dt>
Actually processes the request by instantiating
<tt class=
"member">RequestHandlerClass
</tt> and calling its
<tt class=
"method">handle()
</tt> method.
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
3559' xml:id='l2h-
3559'
class=
"function">get_request
</tt></b>(
</nobr></td>
<td><var></var>)
</td></tr></table></dt>
Must accept a request from the socket, and return a
2-tuple containing
the
<em>new
</em> socket object to be used to communicate with the
client, and the client's address.
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
3560' xml:id='l2h-
3560'
class=
"function">handle_error
</tt></b>(
</nobr></td>
<td><var>request, client_address
</var>)
</td></tr></table></dt>
This function is called if the
<tt class=
"member">RequestHandlerClass
</tt>'s
<tt class=
"method">handle()
</tt> method raises an exception. The default action is
to print the traceback to standard output and continue handling
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
3561' xml:id='l2h-
3561'
class=
"function">process_request
</tt></b>(
</nobr></td>
<td><var>request, client_address
</var>)
</td></tr></table></dt>
Calls
<tt class=
"method">finish_request()
</tt> to create an instance of the
<tt class=
"member">RequestHandlerClass
</tt>. If desired, this function can create a
new process or thread to handle the request; the
<tt class=
"class">ForkingMixIn
</tt>
and
<tt class=
"class">ThreadingMixIn
</tt> classes do this.
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
3562' xml:id='l2h-
3562'
class=
"function">server_activate
</tt></b>(
</nobr></td>
<td><var></var>)
</td></tr></table></dt>
Called by the server's constructor to activate the server. The default
behavior just
<tt class=
"method">listen
</tt>s to the server's socket.
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
3563' xml:id='l2h-
3563'
class=
"function">server_bind
</tt></b>(
</nobr></td>
<td><var></var>)
</td></tr></table></dt>
Called by the server's constructor to bind the socket to the desired
address. May be overridden.
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
3564' xml:id='l2h-
3564'
class=
"function">verify_request
</tt></b>(
</nobr></td>
<td><var>request, client_address
</var>)
</td></tr></table></dt>
Must return a Boolean value; if the value is
<tt class=
"constant">True
</tt>, the request will be
processed, and if it's
<tt class=
"constant">False
</tt>, the request will be denied.
This function can be overridden to implement access controls for a server.
The default implementation always returns
<tt class=
"constant">True
</tt>.
<div class='online-navigation'
>
<table align=
"center" width=
"100%" cellpadding=
"0" cellspacing=
"2">
<td class='online-navigation'
><a rel=
"prev" title=
"11.16.1 Server Creation Notes"
href=
"node535.html"><img src='../icons/previous.png'
border='
0' height='
32' alt='Previous Page' width='
32'
/></A></td>
<td class='online-navigation'
><a rel=
"parent" title=
"11.16 SocketServer "
href=
"module-SocketServer.html"><img src='../icons/up.png'
border='
0' height='
32' alt='Up One Level' width='
32'
/></A></td>
<td class='online-navigation'
><a rel=
"next" title=
"11.16.3 RequestHandler Objects"
href=
"node537.html"><img src='../icons/next.png'
border='
0' height='
32' alt='Next Page' width='
32'
/></A></td>
<td align=
"center" width=
"100%">Python Library Reference
</td>
<td class='online-navigation'
><a rel=
"contents" title=
"Table of Contents"
href=
"contents.html"><img src='../icons/contents.png'
border='
0' height='
32' alt='Contents' width='
32'
/></A></td>
<td class='online-navigation'
><a href=
"modindex.html" title=
"Module Index"><img src='../icons/modules.png'
border='
0' height='
32' alt='Module Index' width='
32'
/></a></td>
<td class='online-navigation'
><a rel=
"index" title=
"Index"
href=
"genindex.html"><img src='../icons/index.png'
border='
0' height='
32' alt='Index' width='
32'
/></A></td>
<div class='online-navigation'
>
<b class=
"navlabel">Previous:
</b>
<a class=
"sectref" rel=
"prev" href=
"node535.html">11.16.1 Server Creation Notes
</A>
<b class=
"navlabel">Up:
</b>
<a class=
"sectref" rel=
"parent" href=
"module-SocketServer.html">11.16 SocketServer
</A>
<b class=
"navlabel">Next:
</b>
<a class=
"sectref" rel=
"next" href=
"node537.html">11.16.3 RequestHandler Objects
</A>
<span class=
"release-info">Release
2.4.2, documentation updated on
28 September
2005.
</span>
<!--End of Navigation Panel-->
See
<i><a href=
"about.html">About this document...
</a></i> for information on suggesting changes.