<!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=
"module-BaseHTTPServer.html" />
<link rel=
"prev" href=
"module-urlparse.html" />
<link rel=
"parent" href=
"internet.html" />
<link rel=
"next" href=
"node535.html" />
<meta name='aesop' content='information'
/>
<title>11.16 SocketServer -- A framework for network servers
</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.15 urlparse "
href=
"module-urlparse.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. Internet Protocols and"
href=
"internet.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.1 Server Creation Notes"
href=
"node535.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=
"module-urlparse.html">11.15 urlparse
</A>
<b class=
"navlabel">Up:
</b>
<a class=
"sectref" rel=
"parent" href=
"internet.html">11. Internet Protocols and
</A>
<b class=
"navlabel">Next:
</b>
<a class=
"sectref" rel=
"next" href=
"node535.html">11.16.1 Server Creation Notes
</A>
<!--End of Navigation Panel-->
<H1><A NAME=
"SECTION00131600000000000000000">
11.16 <tt class=
"module">SocketServer
</tt> --
A framework for network servers
</A>
<A NAME=
"module-SocketServer"></A>
The
<tt class=
"module">SocketServer
</tt> module simplifies the task of writing network
There are four basic server classes:
<tt class=
"class">TCPServer
</tt> uses the
Internet TCP protocol, which provides for continuous streams of data
between the client and server.
<tt class=
"class">UDPServer
</tt> uses datagrams, which
are discrete packets of information that may arrive out of order or be
lost while in transit. The more infrequently used
<tt class=
"class">UnixStreamServer
</tt> and
<tt class=
"class">UnixDatagramServer
</tt> classes are
similar, but use
<span class=
"Unix">Unix
</span> domain sockets; they're not available on
non-
<span class=
"Unix">Unix
</span> platforms. For more details on network programming, consult
a book such as W. Richard Steven's
<em class=
"citetitle"
>UNIX Network Programming
</em>
or Ralph Davis's
<em class=
"citetitle"
>Win32 Network Programming
</em>.
These four classes process requests
<i class=
"dfn">synchronously
</i>; each request
must be completed before the next request can be started. This isn't
suitable if each request takes a long time to complete, because it
requires a lot of computation, or because it returns a lot of data
which the client is slow to process. The solution is to create a
separate process or thread to handle each request; the
<tt class=
"class">ForkingMixIn
</tt> and
<tt class=
"class">ThreadingMixIn
</tt> mix-in classes can be
used to support asynchronous behaviour.
Creating a server requires several steps. First, you must create a
request handler class by subclassing the
<tt class=
"class">BaseRequestHandler
</tt>
class and overriding its
<tt class=
"method">handle()
</tt> method; this method will
process incoming requests. Second, you must instantiate one of the
server classes, passing it the server's address and the request
handler class. Finally, call the
<tt class=
"method">handle_request()
</tt> or
<tt class=
"method">serve_forever()
</tt> method of the server object to process one or
When inheriting from
<tt class=
"class">ThreadingMixIn
</tt> for threaded connection
behavior, you should explicitly declare how you want your threads
to behave on an abrupt shutdown. The
<tt class=
"class">ThreadingMixIn
</tt> class
defines an attribute
<var>daemon_threads
</var>, which indicates whether
or not the server should wait for thread termination. You should
set the flag explicitly if you would like threads to behave
autonomously; the default is
<tt class=
"constant">False
</tt>, meaning that Python
will not exit until all threads created by
<tt class=
"class">ThreadingMixIn
</tt> have
Server classes have the same external methods and attributes, no
matter what network protocol they use:
<p><br /></p><hr class='online-navigation'
/>
<div class='online-navigation'
>
<!--Table of Child-Links-->
<A NAME=
"CHILD_LINKS"><STRONG>Subsections
</STRONG></a>
<LI><A href=
"node535.html">11.16.1 Server Creation Notes
</a>
<LI><A href=
"node536.html">11.16.2 Server Objects
</a>
<LI><A href=
"node537.html">11.16.3 RequestHandler Objects
</a>
<!--End of Table of Child-Links-->
<div class='online-navigation'
>
<table align=
"center" width=
"100%" cellpadding=
"0" cellspacing=
"2">
<td class='online-navigation'
><a rel=
"prev" title=
"11.15 urlparse "
href=
"module-urlparse.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. Internet Protocols and"
href=
"internet.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.1 Server Creation Notes"
href=
"node535.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=
"module-urlparse.html">11.15 urlparse
</A>
<b class=
"navlabel">Up:
</b>
<a class=
"sectref" rel=
"parent" href=
"internet.html">11. Internet Protocols and
</A>
<b class=
"navlabel">Next:
</b>
<a class=
"sectref" rel=
"next" href=
"node535.html">11.16.1 Server Creation Notes
</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.