1 \section{\module{SocketServer
} ---
2 A framework for network servers
}
4 \declaremodule{standard
}{SocketServer
}
5 \modulesynopsis{A framework for network servers.
}
8 The
\module{SocketServer
} module simplifies the task of writing network
11 There are four basic server classes:
\class{TCPServer
} uses the
12 Internet TCP protocol, which provides for continuous streams of data
13 between the client and server.
\class{UDPServer
} uses datagrams, which
14 are discrete packets of information that may arrive out of order or be
15 lost while in transit. The more infrequently used
16 \class{UnixStreamServer
} and
\class{UnixDatagramServer
} classes are
17 similar, but use
\UNIX{} domain sockets; they're not available on
18 non-
\UNIX{} platforms. For more details on network programming, consult
19 a book such as W. Richard Steven's
\citetitle{UNIX Network Programming
}
20 or Ralph Davis's
\citetitle{Win32 Network Programming
}.
22 These four classes process requests
\dfn{synchronously
}; each request
23 must be completed before the next request can be started. This isn't
24 suitable if each request takes a long time to complete, because it
25 requires a lot of computation, or because it returns a lot of data
26 which the client is slow to process. The solution is to create a
27 separate process or thread to handle each request; the
28 \class{ForkingMixIn
} and
\class{ThreadingMixIn
} mix-in classes can be
29 used to support asynchronous behaviour.
31 Creating a server requires several steps. First, you must create a
32 request handler class by subclassing the
\class{BaseRequestHandler
}
33 class and overriding its
\method{handle()
} method; this method will
34 process incoming requests. Second, you must instantiate one of the
35 server classes, passing it the server's address and the request
36 handler class. Finally, call the
\method{handle_request()
} or
37 \method{serve_forever()
} method of the server object to process one or
40 When inheriting from
\class{ThreadingMixIn
} for threaded connection
41 behavior, you should explicitly declare how you want your threads
42 to behave on an abrupt shutdown. The
\class{ThreadingMixIn
} class
43 defines an attribute
\var{daemon_threads
}, which indicates whether
44 or not the server should wait for thread termination. You should
45 set the flag explicitly if you would like threads to behave
46 autonomously; the default is
\constant{False
}, meaning that Python
47 will not exit until all threads created by
\class{ThreadingMixIn
} have
50 Server classes have the same external methods and attributes, no
51 matter what network protocol they use:
53 \setindexsubitem{(SocketServer protocol)
}
55 %XXX should data and methods be intermingled, or separate?
56 % how should the distinction between class and instance variables be
59 \begin{funcdesc
}{fileno
}{}
60 Return an integer file descriptor for the socket on which the server
61 is listening. This function is most commonly passed to
62 \function{select.select()
}, to allow monitoring multiple servers in the
66 \begin{funcdesc
}{handle_request
}{}
67 Process a single request. This function calls the following methods
68 in order:
\method{get_request()
},
\method{verify_request()
}, and
69 \method{process_request()
}. If the user-provided
\method{handle()
}
70 method of the handler class raises an exception, the server's
71 \method{handle_error()
} method will be called.
74 \begin{funcdesc
}{serve_forever
}{}
75 Handle an infinite number of requests. This simply calls
76 \method{handle_request()
} inside an infinite loop.
79 \begin{datadesc
}{address_family
}
80 The family of protocols to which the server's socket belongs.
81 \constant{socket.AF_INET
} and
\constant{socket.AF_UNIX
} are two
85 \begin{datadesc
}{RequestHandlerClass
}
86 The user-provided request handler class; an instance of this class is
87 created for each request.
90 \begin{datadesc
}{server_address
}
91 The address on which the server is listening. The format of addresses
92 varies depending on the protocol family; see the documentation for the
93 socket module for details. For Internet protocols, this is a tuple
94 containing a string giving the address, and an integer port number:
95 \code{('
127.0.0.1',
80)
}, for example.
98 \begin{datadesc
}{socket
}
99 The socket object on which the server will listen for incoming requests.
102 % XXX should class variables be covered before instance variables, or
105 The server classes support the following class variables:
107 \begin{datadesc
}{allow_reuse_address
}
108 Whether the server will allow the reuse of an address. This defaults
109 to
\constant{False
}, and can be set in subclasses to change the policy.
112 \begin{datadesc
}{request_queue_size
}
113 The size of the request queue. If it takes a long time to process a
114 single request, any requests that arrive while the server is busy are
115 placed into a queue, up to
\member{request_queue_size
} requests. Once
116 the queue is full, further requests from clients will get a
117 ``Connection denied'' error. The default value is usually
5, but this
118 can be overridden by subclasses.
121 \begin{datadesc
}{socket_type
}
122 The type of socket used by the server;
\constant{socket.SOCK_STREAM
}
123 and
\constant{socket.SOCK_DGRAM
} are two possible values.
126 There are various server methods that can be overridden by subclasses
127 of base server classes like
\class{TCPServer
}; these methods aren't
128 useful to external users of the server object.
130 % should the default implementations of these be documented, or should
131 % it be assumed that the user will look at SocketServer.py?
133 \begin{funcdesc
}{finish_request
}{}
134 Actually processes the request by instantiating
135 \member{RequestHandlerClass
} and calling its
\method{handle()
} method.
138 \begin{funcdesc
}{get_request
}{}
139 Must accept a request from the socket, and return a
2-tuple containing
140 the
\emph{new
} socket object to be used to communicate with the
141 client, and the client's address.
144 \begin{funcdesc
}{handle_error
}{request, client_address
}
145 This function is called if the
\member{RequestHandlerClass
}'s
146 \method{handle()
} method raises an exception. The default action is
147 to print the traceback to standard output and continue handling
151 \begin{funcdesc
}{process_request
}{request, client_address
}
152 Calls
\method{finish_request()
} to create an instance of the
153 \member{RequestHandlerClass
}. If desired, this function can create a
154 new process or thread to handle the request; the
\class{ForkingMixIn
}
155 and
\class{ThreadingMixIn
} classes do this.
158 % Is there any point in documenting the following two functions?
159 % What would the purpose of overriding them be: initializing server
160 % instance variables, adding new network families?
162 \begin{funcdesc
}{server_activate
}{}
163 Called by the server's constructor to activate the server.
167 \begin{funcdesc
}{server_bind
}{}
168 Called by the server's constructor to bind the socket to the desired
169 address. May be overridden.
172 \begin{funcdesc
}{verify_request
}{request, client_address
}
173 Must return a Boolean value; if the value is
\constant{True
}, the request will be
174 processed, and if it's
\constant{False
}, the request will be denied.
175 This function can be overridden to implement access controls for a server.
176 The default implementation always returns
\constant{True
}.
179 The request handler class must define a new
\method{handle()
} method,
180 and can override any of the following methods. A new instance is
181 created for each request.
183 \begin{funcdesc
}{finish
}{}
184 Called after the
\method{handle()
} method to perform any clean-up
185 actions required. The default implementation does nothing. If
186 \method{setup()
} or
\method{handle()
} raise an exception, this
187 function will not be called.
190 \begin{funcdesc
}{handle
}{}
191 This function must do all the work required to service a request.
192 Several instance attributes are available to it; the request is
193 available as
\member{self.request
}; the client address as
194 \member{self.client_address
}; and the server instance as
195 \member{self.server
}, in case it needs access to per-server
198 The type of
\member{self.request
} is different for datagram or stream
199 services. For stream services,
\member{self.request
} is a socket
200 object; for datagram services,
\member{self.request
} is a string.
201 However, this can be hidden by using the mix-in request handler
203 \class{StreamRequestHandler
} or
\class{DatagramRequestHandler
}, which
204 override the
\method{setup()
} and
\method{finish()
} methods, and
205 provides
\member{self.rfile
} and
\member{self.wfile
} attributes.
206 \member{self.rfile
} and
\member{self.wfile
} can be read or written,
207 respectively, to get the request data or return data to the client.
210 \begin{funcdesc
}{setup
}{}
211 Called before the
\method{handle()
} method to perform any
212 initialization actions required. The default implementation does