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