blob: 1e3cac387b82b6ed206a5de0cdad3df86315fe7a [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
39%XXX should data and methods be intermingled, or separate?
40% how should the distinction between class and instance variables be
41% drawn?
42
43\begin{funcdesc}{fileno}{}
44Return an integer file descriptor for the socket on which the server
45is listening. This function is most commonly passed to
46\code{select.select()}, to allow monitoring multiple servers in the
47same process.
48\end{funcdesc}
49
50\begin{funcdesc}{handle_request}{}
51Process a single request. This function calls the following methods
52in order: \code{get_request()}, \code{verify_request()}, and
53\code{process_request()}. If the user-provided \code{handle()} method
54of the handler class raises an exception, the server's
55\code{handle_error()} method will be called.
56\end{funcdesc}
57
58\begin{funcdesc}{serve_forever}{}
59Handle an infinite number of requests. This simply calls
60\code{handle_request()} inside an infinite loop.
61\end{funcdesc}
62
63\begin{datadesc}{address_family}
64The family of protocols to which the server's socket belongs.
65\code{socket.AF_INET} and \code{socket.AF_UNIX} are two possible values.
66\end{datadesc}
67
68\begin{datadesc}{RequestHandlerClass}
69The user-provided request handler class; an instance of this class is
70created for each request.
71\end{datadesc}
72
73\begin{datadesc}{server_address}
74The address on which the server is listening. The format of addresses
75varies depending on the protocol family; see the documentation for the
76socket module for details. For Internet protocols, this is a tuple
77containing a string giving the address, and an integer port number:
78\code{('127.0.0.1', 80)}, for example.
79\end{datadesc}
80
81\begin{datadesc}{socket}
82The socket object on which the server will listen for incoming requests.
83\end{datadesc}
84
85% XXX should class variables be covered before instance variables, or
86% vice versa?
87
88The server classes support the following class variables:
89
90\begin{datadesc}{request_queue_size}
91The size of the request queue. If it takes a long time to process a
92single request, any requests that arrive while the server is busy are
93placed into a queue, up to \code{request_queue_size} requests. Once
94the queue is full, further requests from clients will get a
95``Connection denied'' error. The default value is usually 5, but this
96can be overridden by subclasses.
97\end{datadesc}
98
99\begin{datadesc}{socket_type}
100The type of socket used by the server; \code{socket.SOCK_STREAM} and
101\code{socket.SOCK_DGRAM} are two possible values.
102\end{datadesc}
103
104There are various server methods that can be overridden by subclasses
105of base server classes like \code{TCPServer}; these methods aren't
106useful to external users of the server object.
107
108% should the default implementations of these be documented, or should
109% it be assumed that the user will look at SocketServer.py?
110
111\begin{funcdesc}{finish_request}{}
112Actually processes the request by instantiating
113\code{RequestHandlerClass} and calling its \code{handle()} method.
114\end{funcdesc}
115
116\begin{funcdesc}{get_request}{}
117Must accept a request from the socket, and return a 2-tuple containing
118the \emph{new} socket object to be used to communicate with the
119client, and the client's address.
120\end{funcdesc}
121
122\begin{funcdesc}{handle_error}{request\, client_address}
123This function is called if the \code{RequestHandlerClass}'s
124\code{handle} method raises an exception. The default action is to print
125the traceback to standard output and continue handling further requests.
126\end{funcdesc}
127
128\begin{funcdesc}{process_request}{request\, client_address}
129Calls \code{finish_request()} to create an instance of the
130\code{RequestHandlerClass}. If desired, this function can create a new
131process or thread to handle the request; the \code{ForkingMixIn} and
132\code{ThreadingMixIn} classes do this.
133\end{funcdesc}
134
135% Is there any point in documenting the following two functions?
136% What would the purpose of overriding them be: initializing server
137% instance variables, adding new network families?
138
139\begin{funcdesc}{server_activate}{}
140Called by the server's constructor to activate the server.
141May be overridden.
142\end{funcdesc}
143
144\begin{funcdesc}{server_bind}{}
145Called by the server's constructor to bind the socket to the desired
146address. May be overridden.
147\end{funcdesc}
148
149\begin{funcdesc}{verify_request}{request\, client_address}
150Must return a Boolean value; if the value is true, the request will be
151processed, and if it's false, the request will be denied.
152This function can be overridden to implement access controls for a server.
153The default implementation always return true.
154\end{funcdesc}
155
156The request handler class must define a new \code{handle} method, and
157can override any of the following methods. A new instance is created
158for each request.
159
160\begin{funcdesc}{finish}{}
161Called after the \code{handle} method to perform any clean-up actions
162required. The default implementation does nothing. If \code{setup()}
163or \code{handle()} raise an exception, this function will not be called.
164\end{funcdesc}
165
166\begin{funcdesc}{handle}{}
167This function must do all the work required to service a request.
168Several instance attributes are available to it; the request is
169available as \code{self.request}; the client address as
170\code{self.client_request}; and the server instance as \code{self.server}, in
171case it needs access to per-server information.
172
173The type of \code{self.request} is different for datagram or stream
174services. For stream services, \code{self.request} is a socket
175object; for datagram services, \code{self.request} is a string.
176However, this can be hidden by using the mix-in request handler
177classes
178\code{StreamRequestHandler} or \code{DatagramRequestHandler}, which
179override the \code{setup} and \code{finish} methods, and provides
180\code{self.rfile} and \code{self.wfile} attributes. \code{self.rfile}
181and \code{self.wfile} can be read or written, respectively, to get the
182request data or return data to the client.
183\end{funcdesc}
184
185\begin{funcdesc}{setup}{}
186Called before the \code{handle} method to perform any initialization
187actions required. The default implementation does nothing.
188\end{funcdesc}