blob: a45b94842f7a3658b8ef960b75623f5a5e8ae032 [file] [log] [blame]
Guido van Rossum6181e001997-05-19 19:55:16 +00001\section{Standard Module \sectcode{SocketServer}}
2\stmodindex{SocketServer}
3
4The \code{SocketServer} module simplifies the task of writing network
5servers.
6
7There are four basic server classes: \code{TCPServer} uses the
8Internet TCP protocol, which provides for continuous streams of data
9between the client and server. \code{UDPServer} uses datagrams, which
10are discrete packets of information that may arrive out of order or be
11lost while in transit. The more infrequently used
12\code{UnixStreamServer} and \code{UnixDatagramServer} classes are
13similar, but use Unix domain sockets; they're not available on
14non-Unix platforms. For more details on network programming, consult
15a book such as W. Richard Steven's \emph{Unix Network Programming}_ or
16XXX (a Windows equivalent).
17
18These four classes process requests \dfn{synchronously}; each request
19must be completed before the next request can be started. This isn't
20suitable if each request takes a long time to complete, because it
21requires a lot of computation, or because it returns a lot of data
22which the client is slow to process. The solution is to create a
23separate process or thread to handle each request; the
24\code{ForkingMixIn} and \code{ThreadingMixIn} mix-in classes can be
25used to support asynchronous behaviour.
26
27Creating a server requires several steps. First, you must create a
28request handler class by subclassing the \code{BaseRequestHandler}
29class and overriding its \code{handle()} method; this method will
30process incoming requests. Second, you must instantiate one of the
31server classes, passing it the server's address and the request
32handler class. Finally, call the \code{handle_request()} or
33\code{serve_forever()} method of the server object to process one or
34many requests.
35
36Server classes have the same external methods and attributes, no
37matter what network protocol they use:
38
Fred Drake798654f1997-11-30 05:53:22 +000039\renewcommand{\indexsubitem}{(in module SocketServer)}
40
Guido van Rossum6181e001997-05-19 19:55:16 +000041%XXX should data and methods be intermingled, or separate?
42% how should the distinction between class and instance variables be
43% drawn?
44
45\begin{funcdesc}{fileno}{}
46Return an integer file descriptor for the socket on which the server
47is listening. This function is most commonly passed to
48\code{select.select()}, to allow monitoring multiple servers in the
49same process.
50\end{funcdesc}
51
52\begin{funcdesc}{handle_request}{}
53Process a single request. This function calls the following methods
54in order: \code{get_request()}, \code{verify_request()}, and
55\code{process_request()}. If the user-provided \code{handle()} method
56of the handler class raises an exception, the server's
57\code{handle_error()} method will be called.
58\end{funcdesc}
59
60\begin{funcdesc}{serve_forever}{}
61Handle an infinite number of requests. This simply calls
62\code{handle_request()} inside an infinite loop.
63\end{funcdesc}
64
65\begin{datadesc}{address_family}
66The family of protocols to which the server's socket belongs.
67\code{socket.AF_INET} and \code{socket.AF_UNIX} are two possible values.
68\end{datadesc}
69
70\begin{datadesc}{RequestHandlerClass}
71The user-provided request handler class; an instance of this class is
72created for each request.
73\end{datadesc}
74
75\begin{datadesc}{server_address}
76The address on which the server is listening. The format of addresses
77varies depending on the protocol family; see the documentation for the
78socket module for details. For Internet protocols, this is a tuple
79containing a string giving the address, and an integer port number:
80\code{('127.0.0.1', 80)}, for example.
81\end{datadesc}
82
83\begin{datadesc}{socket}
84The socket object on which the server will listen for incoming requests.
85\end{datadesc}
86
87% XXX should class variables be covered before instance variables, or
88% vice versa?
89
90The server classes support the following class variables:
91
92\begin{datadesc}{request_queue_size}
93The size of the request queue. If it takes a long time to process a
94single request, any requests that arrive while the server is busy are
95placed into a queue, up to \code{request_queue_size} requests. Once
96the queue is full, further requests from clients will get a
97``Connection denied'' error. The default value is usually 5, but this
98can be overridden by subclasses.
99\end{datadesc}
100
101\begin{datadesc}{socket_type}
102The type of socket used by the server; \code{socket.SOCK_STREAM} and
103\code{socket.SOCK_DGRAM} are two possible values.
104\end{datadesc}
105
106There are various server methods that can be overridden by subclasses
107of base server classes like \code{TCPServer}; these methods aren't
108useful to external users of the server object.
109
110% should the default implementations of these be documented, or should
111% it be assumed that the user will look at SocketServer.py?
112
113\begin{funcdesc}{finish_request}{}
114Actually processes the request by instantiating
115\code{RequestHandlerClass} and calling its \code{handle()} method.
116\end{funcdesc}
117
118\begin{funcdesc}{get_request}{}
119Must accept a request from the socket, and return a 2-tuple containing
120the \emph{new} socket object to be used to communicate with the
121client, and the client's address.
122\end{funcdesc}
123
124\begin{funcdesc}{handle_error}{request\, client_address}
125This function is called if the \code{RequestHandlerClass}'s
126\code{handle} method raises an exception. The default action is to print
127the traceback to standard output and continue handling further requests.
128\end{funcdesc}
129
130\begin{funcdesc}{process_request}{request\, client_address}
131Calls \code{finish_request()} to create an instance of the
132\code{RequestHandlerClass}. If desired, this function can create a new
133process or thread to handle the request; the \code{ForkingMixIn} and
134\code{ThreadingMixIn} classes do this.
135\end{funcdesc}
136
137% Is there any point in documenting the following two functions?
138% What would the purpose of overriding them be: initializing server
139% instance variables, adding new network families?
140
141\begin{funcdesc}{server_activate}{}
142Called by the server's constructor to activate the server.
143May be overridden.
144\end{funcdesc}
145
146\begin{funcdesc}{server_bind}{}
147Called by the server's constructor to bind the socket to the desired
148address. May be overridden.
149\end{funcdesc}
150
151\begin{funcdesc}{verify_request}{request\, client_address}
152Must return a Boolean value; if the value is true, the request will be
153processed, and if it's false, the request will be denied.
154This function can be overridden to implement access controls for a server.
155The default implementation always return true.
156\end{funcdesc}
157
158The request handler class must define a new \code{handle} method, and
159can override any of the following methods. A new instance is created
160for each request.
161
162\begin{funcdesc}{finish}{}
163Called after the \code{handle} method to perform any clean-up actions
164required. The default implementation does nothing. If \code{setup()}
165or \code{handle()} raise an exception, this function will not be called.
166\end{funcdesc}
167
168\begin{funcdesc}{handle}{}
169This function must do all the work required to service a request.
170Several instance attributes are available to it; the request is
171available as \code{self.request}; the client address as
172\code{self.client_request}; and the server instance as \code{self.server}, in
173case it needs access to per-server information.
174
175The type of \code{self.request} is different for datagram or stream
176services. For stream services, \code{self.request} is a socket
177object; for datagram services, \code{self.request} is a string.
178However, this can be hidden by using the mix-in request handler
179classes
180\code{StreamRequestHandler} or \code{DatagramRequestHandler}, which
181override the \code{setup} and \code{finish} methods, and provides
182\code{self.rfile} and \code{self.wfile} attributes. \code{self.rfile}
183and \code{self.wfile} can be read or written, respectively, to get the
184request data or return data to the client.
185\end{funcdesc}
186
187\begin{funcdesc}{setup}{}
188Called before the \code{handle} method to perform any initialization
189actions required. The default implementation does nothing.
190\end{funcdesc}