blob: ea1b703f8aeb9c65d4a941f49593c82433db7160 [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
Fred Drakebe2b6d71998-03-14 06:40:34 +00005The \module{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
Fred Drakebe2b6d71998-03-14 06:40:34 +00008There are four basic server classes: \class{TCPServer} uses the
Guido van Rossum6181e001997-05-19 19:55:16 +00009Internet TCP protocol, which provides for continuous streams of data
Fred Drakebe2b6d71998-03-14 06:40:34 +000010between the client and server. \class{UDPServer} uses datagrams, which
Guido van Rossum6181e001997-05-19 19:55:16 +000011are discrete packets of information that may arrive out of order or be
12lost while in transit. The more infrequently used
Fred Drakebe2b6d71998-03-14 06:40:34 +000013\class{UnixStreamServer} and \class{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
Fred Drakebe2b6d71998-03-14 06:40:34 +000025\class{ForkingMixIn} and \class{ThreadingMixIn} mix-in classes can be
Guido van Rossum6181e001997-05-19 19:55:16 +000026used to support asynchronous behaviour.
27
28Creating a server requires several steps. First, you must create a
Fred Drakebe2b6d71998-03-14 06:40:34 +000029request handler class by subclassing the \class{BaseRequestHandler}
30class and overriding its \method{handle()} method; this method will
Guido van Rossum6181e001997-05-19 19:55:16 +000031process incoming requests. Second, you must instantiate one of the
32server classes, passing it the server's address and the request
Fred Drakebe2b6d71998-03-14 06:40:34 +000033handler class. Finally, call the \method{handle_request()} or
34\method{serve_forever()} method of the server object to process one or
Guido van Rossum6181e001997-05-19 19:55:16 +000035many 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
Fred Drakebe2b6d71998-03-14 06:40:34 +000049\function{select.select()}, to allow monitoring multiple servers in the
Guido van Rossum6181e001997-05-19 19:55:16 +000050same process.
51\end{funcdesc}
52
53\begin{funcdesc}{handle_request}{}
54Process a single request. This function calls the following methods
Fred Drakebe2b6d71998-03-14 06:40:34 +000055in order: \method{get_request()}, \method{verify_request()}, and
56\method{process_request()}. If the user-provided \method{handle()}
57method of the handler class raises an exception, the server's
58\method{handle_error()} method will be called.
Guido van Rossum6181e001997-05-19 19:55:16 +000059\end{funcdesc}
60
61\begin{funcdesc}{serve_forever}{}
62Handle an infinite number of requests. This simply calls
Fred Drakebe2b6d71998-03-14 06:40:34 +000063\method{handle_request()} inside an infinite loop.
Guido van Rossum6181e001997-05-19 19:55:16 +000064\end{funcdesc}
65
66\begin{datadesc}{address_family}
67The family of protocols to which the server's socket belongs.
Fred Drakebe2b6d71998-03-14 06:40:34 +000068\constant{socket.AF_INET} and \constant{socket.AF_UNIX} are two
69possible values.
Guido van Rossum6181e001997-05-19 19:55:16 +000070\end{datadesc}
71
72\begin{datadesc}{RequestHandlerClass}
73The user-provided request handler class; an instance of this class is
74created for each request.
75\end{datadesc}
76
77\begin{datadesc}{server_address}
78The address on which the server is listening. The format of addresses
79varies depending on the protocol family; see the documentation for the
80socket module for details. For Internet protocols, this is a tuple
81containing a string giving the address, and an integer port number:
82\code{('127.0.0.1', 80)}, for example.
83\end{datadesc}
84
85\begin{datadesc}{socket}
86The socket object on which the server will listen for incoming requests.
87\end{datadesc}
88
89% XXX should class variables be covered before instance variables, or
90% vice versa?
91
92The server classes support the following class variables:
93
94\begin{datadesc}{request_queue_size}
95The size of the request queue. If it takes a long time to process a
96single request, any requests that arrive while the server is busy are
Fred Drakebe2b6d71998-03-14 06:40:34 +000097placed into a queue, up to \member{request_queue_size} requests. Once
Guido van Rossum6181e001997-05-19 19:55:16 +000098the queue is full, further requests from clients will get a
99``Connection denied'' error. The default value is usually 5, but this
100can be overridden by subclasses.
101\end{datadesc}
102
103\begin{datadesc}{socket_type}
Fred Drakebe2b6d71998-03-14 06:40:34 +0000104The type of socket used by the server; \constant{socket.SOCK_STREAM}
105and \constant{socket.SOCK_DGRAM} are two possible values.
Guido van Rossum6181e001997-05-19 19:55:16 +0000106\end{datadesc}
107
108There are various server methods that can be overridden by subclasses
Fred Drakebe2b6d71998-03-14 06:40:34 +0000109of base server classes like \class{TCPServer}; these methods aren't
Guido van Rossum6181e001997-05-19 19:55:16 +0000110useful to external users of the server object.
111
112% should the default implementations of these be documented, or should
113% it be assumed that the user will look at SocketServer.py?
114
115\begin{funcdesc}{finish_request}{}
116Actually processes the request by instantiating
Fred Drakebe2b6d71998-03-14 06:40:34 +0000117\member{RequestHandlerClass} and calling its \method{handle()} method.
Guido van Rossum6181e001997-05-19 19:55:16 +0000118\end{funcdesc}
119
120\begin{funcdesc}{get_request}{}
121Must accept a request from the socket, and return a 2-tuple containing
122the \emph{new} socket object to be used to communicate with the
123client, and the client's address.
124\end{funcdesc}
125
126\begin{funcdesc}{handle_error}{request\, client_address}
Fred Drakebe2b6d71998-03-14 06:40:34 +0000127This function is called if the \member{RequestHandlerClass}'s
128\method{handle()} method raises an exception. The default action is
129to print the traceback to standard output and continue handling
130further requests.
Guido van Rossum6181e001997-05-19 19:55:16 +0000131\end{funcdesc}
132
133\begin{funcdesc}{process_request}{request\, client_address}
Fred Drakebe2b6d71998-03-14 06:40:34 +0000134Calls \method{finish_request()} to create an instance of the
135\member{RequestHandlerClass}. If desired, this function can create a
136new process or thread to handle the request; the \class{ForkingMixIn}
137and \class{ThreadingMixIn} classes do this.
Guido van Rossum6181e001997-05-19 19:55:16 +0000138\end{funcdesc}
139
140% Is there any point in documenting the following two functions?
141% What would the purpose of overriding them be: initializing server
142% instance variables, adding new network families?
143
144\begin{funcdesc}{server_activate}{}
145Called by the server's constructor to activate the server.
146May be overridden.
147\end{funcdesc}
148
149\begin{funcdesc}{server_bind}{}
150Called by the server's constructor to bind the socket to the desired
151address. May be overridden.
152\end{funcdesc}
153
154\begin{funcdesc}{verify_request}{request\, client_address}
155Must return a Boolean value; if the value is true, the request will be
156processed, and if it's false, the request will be denied.
157This function can be overridden to implement access controls for a server.
158The default implementation always return true.
159\end{funcdesc}
160
Fred Drakebe2b6d71998-03-14 06:40:34 +0000161The request handler class must define a new \method{handle()} method,
162and can override any of the following methods. A new instance is
163created for each request.
Guido van Rossum6181e001997-05-19 19:55:16 +0000164
165\begin{funcdesc}{finish}{}
Fred Drakebe2b6d71998-03-14 06:40:34 +0000166Called after the \method{handle()} method to perform any clean-up
167actions required. The default implementation does nothing. If
168\method{setup()} or \method{handle()} raise an exception, this
169function will not be called.
Guido van Rossum6181e001997-05-19 19:55:16 +0000170\end{funcdesc}
171
172\begin{funcdesc}{handle}{}
173This function must do all the work required to service a request.
174Several instance attributes are available to it; the request is
Fred Drakebe2b6d71998-03-14 06:40:34 +0000175available as \member{self.request}; the client address as
176\member{self.client_request}; and the server instance as
177\member{self.server}, in case it needs access to per-server
178information.
Guido van Rossum6181e001997-05-19 19:55:16 +0000179
Fred Drakebe2b6d71998-03-14 06:40:34 +0000180The type of \member{self.request} is different for datagram or stream
181services. For stream services, \member{self.request} is a socket
182object; for datagram services, \member{self.request} is a string.
Guido van Rossum6181e001997-05-19 19:55:16 +0000183However, this can be hidden by using the mix-in request handler
184classes
Fred Drakebe2b6d71998-03-14 06:40:34 +0000185\class{StreamRequestHandler} or \class{DatagramRequestHandler}, which
186override the \method{setup()} and \method{finish()} methods, and
187provides \member{self.rfile} and \member{self.wfile} attributes.
188\member{self.rfile} and \member{self.wfile} can be read or written,
189respectively, to get the request data or return data to the client.
Guido van Rossum6181e001997-05-19 19:55:16 +0000190\end{funcdesc}
191
192\begin{funcdesc}{setup}{}
Fred Drakebe2b6d71998-03-14 06:40:34 +0000193Called before the \method{handle()} method to perform any
194initialization actions required. The default implementation does
195nothing.
Guido van Rossum6181e001997-05-19 19:55:16 +0000196\end{funcdesc}