| 1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
| 2 | <html> |
| 3 | <head> |
| 4 | <link rel="STYLESHEET" href="lib.css" type='text/css' /> |
| 5 | <link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" /> |
| 6 | <link rel='start' href='../index.html' title='Python Documentation Index' /> |
| 7 | <link rel="first" href="lib.html" title='Python Library Reference' /> |
| 8 | <link rel='contents' href='contents.html' title="Contents" /> |
| 9 | <link rel='index' href='genindex.html' title='Index' /> |
| 10 | <link rel='last' href='about.html' title='About this document...' /> |
| 11 | <link rel='help' href='about.html' title='About this document...' /> |
| 12 | <link rel="next" href="module-BaseHTTPServer.html" /> |
| 13 | <link rel="prev" href="module-urlparse.html" /> |
| 14 | <link rel="parent" href="internet.html" /> |
| 15 | <link rel="next" href="node535.html" /> |
| 16 | <meta name='aesop' content='information' /> |
| 17 | <title>11.16 SocketServer -- A framework for network servers</title> |
| 18 | </head> |
| 19 | <body> |
| 20 | <DIV CLASS="navigation"> |
| 21 | <div id='top-navigation-panel' xml:id='top-navigation-panel'> |
| 22 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 23 | <tr> |
| 24 | <td class='online-navigation'><a rel="prev" title="11.15 urlparse " |
| 25 | href="module-urlparse.html"><img src='../icons/previous.png' |
| 26 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 27 | <td class='online-navigation'><a rel="parent" title="11. Internet Protocols and" |
| 28 | href="internet.html"><img src='../icons/up.png' |
| 29 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 30 | <td class='online-navigation'><a rel="next" title="11.16.1 Server Creation Notes" |
| 31 | href="node535.html"><img src='../icons/next.png' |
| 32 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 33 | <td align="center" width="100%">Python Library Reference</td> |
| 34 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 35 | href="contents.html"><img src='../icons/contents.png' |
| 36 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 37 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' |
| 38 | border='0' height='32' alt='Module Index' width='32' /></a></td> |
| 39 | <td class='online-navigation'><a rel="index" title="Index" |
| 40 | href="genindex.html"><img src='../icons/index.png' |
| 41 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 42 | </tr></table> |
| 43 | <div class='online-navigation'> |
| 44 | <b class="navlabel">Previous:</b> |
| 45 | <a class="sectref" rel="prev" href="module-urlparse.html">11.15 urlparse </A> |
| 46 | <b class="navlabel">Up:</b> |
| 47 | <a class="sectref" rel="parent" href="internet.html">11. Internet Protocols and</A> |
| 48 | <b class="navlabel">Next:</b> |
| 49 | <a class="sectref" rel="next" href="node535.html">11.16.1 Server Creation Notes</A> |
| 50 | </div> |
| 51 | <hr /></div> |
| 52 | </DIV> |
| 53 | <!--End of Navigation Panel--> |
| 54 | |
| 55 | <H1><A NAME="SECTION00131600000000000000000"> |
| 56 | 11.16 <tt class="module">SocketServer</tt> -- |
| 57 | A framework for network servers</A> |
| 58 | </H1> |
| 59 | |
| 60 | <P> |
| 61 | <A NAME="module-SocketServer"></A> |
| 62 | |
| 63 | <P> |
| 64 | The <tt class="module">SocketServer</tt> module simplifies the task of writing network |
| 65 | servers. |
| 66 | |
| 67 | <P> |
| 68 | There are four basic server classes: <tt class="class">TCPServer</tt> uses the |
| 69 | Internet TCP protocol, which provides for continuous streams of data |
| 70 | between the client and server. <tt class="class">UDPServer</tt> uses datagrams, which |
| 71 | are discrete packets of information that may arrive out of order or be |
| 72 | lost while in transit. The more infrequently used |
| 73 | <tt class="class">UnixStreamServer</tt> and <tt class="class">UnixDatagramServer</tt> classes are |
| 74 | similar, but use <span class="Unix">Unix</span> domain sockets; they're not available on |
| 75 | non-<span class="Unix">Unix</span> platforms. For more details on network programming, consult |
| 76 | a book such as W. Richard Steven's <em class="citetitle" |
| 77 | >UNIX Network Programming</em> |
| 78 | or Ralph Davis's <em class="citetitle" |
| 79 | >Win32 Network Programming</em>. |
| 80 | |
| 81 | <P> |
| 82 | These four classes process requests <i class="dfn">synchronously</i>; each request |
| 83 | must be completed before the next request can be started. This isn't |
| 84 | suitable if each request takes a long time to complete, because it |
| 85 | requires a lot of computation, or because it returns a lot of data |
| 86 | which the client is slow to process. The solution is to create a |
| 87 | separate process or thread to handle each request; the |
| 88 | <tt class="class">ForkingMixIn</tt> and <tt class="class">ThreadingMixIn</tt> mix-in classes can be |
| 89 | used to support asynchronous behaviour. |
| 90 | |
| 91 | <P> |
| 92 | Creating a server requires several steps. First, you must create a |
| 93 | request handler class by subclassing the <tt class="class">BaseRequestHandler</tt> |
| 94 | class and overriding its <tt class="method">handle()</tt> method; this method will |
| 95 | process incoming requests. Second, you must instantiate one of the |
| 96 | server classes, passing it the server's address and the request |
| 97 | handler class. Finally, call the <tt class="method">handle_request()</tt> or |
| 98 | <tt class="method">serve_forever()</tt> method of the server object to process one or |
| 99 | many requests. |
| 100 | |
| 101 | <P> |
| 102 | When inheriting from <tt class="class">ThreadingMixIn</tt> for threaded connection |
| 103 | behavior, you should explicitly declare how you want your threads |
| 104 | to behave on an abrupt shutdown. The <tt class="class">ThreadingMixIn</tt> class |
| 105 | defines an attribute <var>daemon_threads</var>, which indicates whether |
| 106 | or not the server should wait for thread termination. You should |
| 107 | set the flag explicitly if you would like threads to behave |
| 108 | autonomously; the default is <tt class="constant">False</tt>, meaning that Python |
| 109 | will not exit until all threads created by <tt class="class">ThreadingMixIn</tt> have |
| 110 | exited. |
| 111 | |
| 112 | <P> |
| 113 | Server classes have the same external methods and attributes, no |
| 114 | matter what network protocol they use: |
| 115 | |
| 116 | <P> |
| 117 | |
| 118 | <P> |
| 119 | |
| 120 | <p><br /></p><hr class='online-navigation' /> |
| 121 | <div class='online-navigation'> |
| 122 | <!--Table of Child-Links--> |
| 123 | <A NAME="CHILD_LINKS"><STRONG>Subsections</STRONG></a> |
| 124 | |
| 125 | <UL CLASS="ChildLinks"> |
| 126 | <LI><A href="node535.html">11.16.1 Server Creation Notes</a> |
| 127 | <LI><A href="node536.html">11.16.2 Server Objects</a> |
| 128 | <LI><A href="node537.html">11.16.3 RequestHandler Objects</a> |
| 129 | </ul> |
| 130 | <!--End of Table of Child-Links--> |
| 131 | </div> |
| 132 | |
| 133 | <DIV CLASS="navigation"> |
| 134 | <div class='online-navigation'> |
| 135 | <p></p><hr /> |
| 136 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 137 | <tr> |
| 138 | <td class='online-navigation'><a rel="prev" title="11.15 urlparse " |
| 139 | href="module-urlparse.html"><img src='../icons/previous.png' |
| 140 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 141 | <td class='online-navigation'><a rel="parent" title="11. Internet Protocols and" |
| 142 | href="internet.html"><img src='../icons/up.png' |
| 143 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 144 | <td class='online-navigation'><a rel="next" title="11.16.1 Server Creation Notes" |
| 145 | href="node535.html"><img src='../icons/next.png' |
| 146 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 147 | <td align="center" width="100%">Python Library Reference</td> |
| 148 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 149 | href="contents.html"><img src='../icons/contents.png' |
| 150 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 151 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' |
| 152 | border='0' height='32' alt='Module Index' width='32' /></a></td> |
| 153 | <td class='online-navigation'><a rel="index" title="Index" |
| 154 | href="genindex.html"><img src='../icons/index.png' |
| 155 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 156 | </tr></table> |
| 157 | <div class='online-navigation'> |
| 158 | <b class="navlabel">Previous:</b> |
| 159 | <a class="sectref" rel="prev" href="module-urlparse.html">11.15 urlparse </A> |
| 160 | <b class="navlabel">Up:</b> |
| 161 | <a class="sectref" rel="parent" href="internet.html">11. Internet Protocols and</A> |
| 162 | <b class="navlabel">Next:</b> |
| 163 | <a class="sectref" rel="next" href="node535.html">11.16.1 Server Creation Notes</A> |
| 164 | </div> |
| 165 | </div> |
| 166 | <hr /> |
| 167 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> |
| 168 | </DIV> |
| 169 | <!--End of Navigation Panel--> |
| 170 | <ADDRESS> |
| 171 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. |
| 172 | </ADDRESS> |
| 173 | </BODY> |
| 174 | </HTML> |