Commit | Line | Data |
---|---|---|
920dae64 AT |
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="node537.html" /> | |
13 | <link rel="prev" href="node535.html" /> | |
14 | <link rel="parent" href="module-SocketServer.html" /> | |
15 | <link rel="next" href="node537.html" /> | |
16 | <meta name='aesop' content='information' /> | |
17 | <title>11.16.2 Server Objects</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.16.1 Server Creation Notes" | |
25 | href="node535.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.16 SocketServer " | |
28 | href="module-SocketServer.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.3 RequestHandler Objects" | |
31 | href="node537.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="node535.html">11.16.1 Server Creation Notes</A> | |
46 | <b class="navlabel">Up:</b> | |
47 | <a class="sectref" rel="parent" href="module-SocketServer.html">11.16 SocketServer </A> | |
48 | <b class="navlabel">Next:</b> | |
49 | <a class="sectref" rel="next" href="node537.html">11.16.3 RequestHandler Objects</A> | |
50 | </div> | |
51 | <hr /></div> | |
52 | </DIV> | |
53 | <!--End of Navigation Panel--> | |
54 | ||
55 | <H2><A NAME="SECTION00131620000000000000000"> | |
56 | 11.16.2 Server Objects</A> | |
57 | </H2> | |
58 | ||
59 | <P> | |
60 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
61 | <td><nobr><b><tt id='l2h-3548' xml:id='l2h-3548' class="function">fileno</tt></b>(</nobr></td> | |
62 | <td><var></var>)</td></tr></table></dt> | |
63 | <dd> | |
64 | Return an integer file descriptor for the socket on which the server | |
65 | is listening. This function is most commonly passed to | |
66 | <tt class="function">select.select()</tt>, to allow monitoring multiple servers in the | |
67 | same process. | |
68 | </dl> | |
69 | ||
70 | <P> | |
71 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
72 | <td><nobr><b><tt id='l2h-3549' xml:id='l2h-3549' class="function">handle_request</tt></b>(</nobr></td> | |
73 | <td><var></var>)</td></tr></table></dt> | |
74 | <dd> | |
75 | Process a single request. This function calls the following methods | |
76 | in order: <tt class="method">get_request()</tt>, <tt class="method">verify_request()</tt>, and | |
77 | <tt class="method">process_request()</tt>. If the user-provided <tt class="method">handle()</tt> | |
78 | method of the handler class raises an exception, the server's | |
79 | <tt class="method">handle_error()</tt> method will be called. | |
80 | </dl> | |
81 | ||
82 | <P> | |
83 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
84 | <td><nobr><b><tt id='l2h-3550' xml:id='l2h-3550' class="function">serve_forever</tt></b>(</nobr></td> | |
85 | <td><var></var>)</td></tr></table></dt> | |
86 | <dd> | |
87 | Handle an infinite number of requests. This simply calls | |
88 | <tt class="method">handle_request()</tt> inside an infinite loop. | |
89 | </dl> | |
90 | ||
91 | <P> | |
92 | <dl><dt><b><tt id='l2h-3551' xml:id='l2h-3551'>address_family</tt></b></dt> | |
93 | <dd> | |
94 | The family of protocols to which the server's socket belongs. | |
95 | <tt class="constant">socket.AF_INET</tt> and <tt class="constant">socket.AF_UNIX</tt> are two | |
96 | possible values. | |
97 | </dd></dl> | |
98 | ||
99 | <P> | |
100 | <dl><dt><b><tt id='l2h-3552' xml:id='l2h-3552'>RequestHandlerClass</tt></b></dt> | |
101 | <dd> | |
102 | The user-provided request handler class; an instance of this class is | |
103 | created for each request. | |
104 | </dd></dl> | |
105 | ||
106 | <P> | |
107 | <dl><dt><b><tt id='l2h-3553' xml:id='l2h-3553'>server_address</tt></b></dt> | |
108 | <dd> | |
109 | The address on which the server is listening. The format of addresses | |
110 | varies depending on the protocol family; see the documentation for the | |
111 | socket module for details. For Internet protocols, this is a tuple | |
112 | containing a string giving the address, and an integer port number: | |
113 | <code>('127.0.0.1', 80)</code>, for example. | |
114 | </dd></dl> | |
115 | ||
116 | <P> | |
117 | <dl><dt><b><tt id='l2h-3554' xml:id='l2h-3554'>socket</tt></b></dt> | |
118 | <dd> | |
119 | The socket object on which the server will listen for incoming requests. | |
120 | </dd></dl> | |
121 | ||
122 | <P> | |
123 | The server classes support the following class variables: | |
124 | ||
125 | <P> | |
126 | <dl><dt><b><tt id='l2h-3555' xml:id='l2h-3555'>allow_reuse_address</tt></b></dt> | |
127 | <dd> | |
128 | Whether the server will allow the reuse of an address. This defaults | |
129 | to <tt class="constant">False</tt>, and can be set in subclasses to change the policy. | |
130 | </dd></dl> | |
131 | ||
132 | <P> | |
133 | <dl><dt><b><tt id='l2h-3556' xml:id='l2h-3556'>request_queue_size</tt></b></dt> | |
134 | <dd> | |
135 | The size of the request queue. If it takes a long time to process a | |
136 | single request, any requests that arrive while the server is busy are | |
137 | placed into a queue, up to <tt class="member">request_queue_size</tt> requests. Once | |
138 | the queue is full, further requests from clients will get a | |
139 | ``Connection denied'' error. The default value is usually 5, but this | |
140 | can be overridden by subclasses. | |
141 | </dd></dl> | |
142 | ||
143 | <P> | |
144 | <dl><dt><b><tt id='l2h-3557' xml:id='l2h-3557'>socket_type</tt></b></dt> | |
145 | <dd> | |
146 | The type of socket used by the server; <tt class="constant">socket.SOCK_STREAM</tt> | |
147 | and <tt class="constant">socket.SOCK_DGRAM</tt> are two possible values. | |
148 | </dd></dl> | |
149 | ||
150 | <P> | |
151 | There are various server methods that can be overridden by subclasses | |
152 | of base server classes like <tt class="class">TCPServer</tt>; these methods aren't | |
153 | useful to external users of the server object. | |
154 | ||
155 | <P> | |
156 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
157 | <td><nobr><b><tt id='l2h-3558' xml:id='l2h-3558' class="function">finish_request</tt></b>(</nobr></td> | |
158 | <td><var></var>)</td></tr></table></dt> | |
159 | <dd> | |
160 | Actually processes the request by instantiating | |
161 | <tt class="member">RequestHandlerClass</tt> and calling its <tt class="method">handle()</tt> method. | |
162 | </dl> | |
163 | ||
164 | <P> | |
165 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
166 | <td><nobr><b><tt id='l2h-3559' xml:id='l2h-3559' class="function">get_request</tt></b>(</nobr></td> | |
167 | <td><var></var>)</td></tr></table></dt> | |
168 | <dd> | |
169 | Must accept a request from the socket, and return a 2-tuple containing | |
170 | the <em>new</em> socket object to be used to communicate with the | |
171 | client, and the client's address. | |
172 | </dl> | |
173 | ||
174 | <P> | |
175 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
176 | <td><nobr><b><tt id='l2h-3560' xml:id='l2h-3560' class="function">handle_error</tt></b>(</nobr></td> | |
177 | <td><var>request, client_address</var>)</td></tr></table></dt> | |
178 | <dd> | |
179 | This function is called if the <tt class="member">RequestHandlerClass</tt>'s | |
180 | <tt class="method">handle()</tt> method raises an exception. The default action is | |
181 | to print the traceback to standard output and continue handling | |
182 | further requests. | |
183 | </dl> | |
184 | ||
185 | <P> | |
186 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
187 | <td><nobr><b><tt id='l2h-3561' xml:id='l2h-3561' class="function">process_request</tt></b>(</nobr></td> | |
188 | <td><var>request, client_address</var>)</td></tr></table></dt> | |
189 | <dd> | |
190 | Calls <tt class="method">finish_request()</tt> to create an instance of the | |
191 | <tt class="member">RequestHandlerClass</tt>. If desired, this function can create a | |
192 | new process or thread to handle the request; the <tt class="class">ForkingMixIn</tt> | |
193 | and <tt class="class">ThreadingMixIn</tt> classes do this. | |
194 | </dl> | |
195 | ||
196 | <P> | |
197 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
198 | <td><nobr><b><tt id='l2h-3562' xml:id='l2h-3562' class="function">server_activate</tt></b>(</nobr></td> | |
199 | <td><var></var>)</td></tr></table></dt> | |
200 | <dd> | |
201 | Called by the server's constructor to activate the server. The default | |
202 | behavior just <tt class="method">listen</tt>s to the server's socket. | |
203 | May be overridden. | |
204 | </dl> | |
205 | ||
206 | <P> | |
207 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
208 | <td><nobr><b><tt id='l2h-3563' xml:id='l2h-3563' class="function">server_bind</tt></b>(</nobr></td> | |
209 | <td><var></var>)</td></tr></table></dt> | |
210 | <dd> | |
211 | Called by the server's constructor to bind the socket to the desired | |
212 | address. May be overridden. | |
213 | </dl> | |
214 | ||
215 | <P> | |
216 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
217 | <td><nobr><b><tt id='l2h-3564' xml:id='l2h-3564' class="function">verify_request</tt></b>(</nobr></td> | |
218 | <td><var>request, client_address</var>)</td></tr></table></dt> | |
219 | <dd> | |
220 | Must return a Boolean value; if the value is <tt class="constant">True</tt>, the request will be | |
221 | processed, and if it's <tt class="constant">False</tt>, the request will be denied. | |
222 | This function can be overridden to implement access controls for a server. | |
223 | The default implementation always returns <tt class="constant">True</tt>. | |
224 | </dl> | |
225 | ||
226 | <P> | |
227 | ||
228 | <DIV CLASS="navigation"> | |
229 | <div class='online-navigation'> | |
230 | <p></p><hr /> | |
231 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
232 | <tr> | |
233 | <td class='online-navigation'><a rel="prev" title="11.16.1 Server Creation Notes" | |
234 | href="node535.html"><img src='../icons/previous.png' | |
235 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
236 | <td class='online-navigation'><a rel="parent" title="11.16 SocketServer " | |
237 | href="module-SocketServer.html"><img src='../icons/up.png' | |
238 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
239 | <td class='online-navigation'><a rel="next" title="11.16.3 RequestHandler Objects" | |
240 | href="node537.html"><img src='../icons/next.png' | |
241 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
242 | <td align="center" width="100%">Python Library Reference</td> | |
243 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
244 | href="contents.html"><img src='../icons/contents.png' | |
245 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
246 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' | |
247 | border='0' height='32' alt='Module Index' width='32' /></a></td> | |
248 | <td class='online-navigation'><a rel="index" title="Index" | |
249 | href="genindex.html"><img src='../icons/index.png' | |
250 | border='0' height='32' alt='Index' width='32' /></A></td> | |
251 | </tr></table> | |
252 | <div class='online-navigation'> | |
253 | <b class="navlabel">Previous:</b> | |
254 | <a class="sectref" rel="prev" href="node535.html">11.16.1 Server Creation Notes</A> | |
255 | <b class="navlabel">Up:</b> | |
256 | <a class="sectref" rel="parent" href="module-SocketServer.html">11.16 SocketServer </A> | |
257 | <b class="navlabel">Next:</b> | |
258 | <a class="sectref" rel="next" href="node537.html">11.16.3 RequestHandler Objects</A> | |
259 | </div> | |
260 | </div> | |
261 | <hr /> | |
262 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> | |
263 | </DIV> | |
264 | <!--End of Navigation Panel--> | |
265 | <ADDRESS> | |
266 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. | |
267 | </ADDRESS> | |
268 | </BODY> | |
269 | </HTML> |