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