|
| 1 | +\section{Standard Module \sectcode{SocketServer}} |
| 2 | +\stmodindex{SocketServer} |
| 3 | + |
| 4 | +The \code{SocketServer} module simplifies the task of writing network |
| 5 | +servers. |
| 6 | + |
| 7 | +There are four basic server classes: \code{TCPServer} uses the |
| 8 | +Internet TCP protocol, which provides for continuous streams of data |
| 9 | +between the client and server. \code{UDPServer} uses datagrams, which |
| 10 | +are discrete packets of information that may arrive out of order or be |
| 11 | +lost while in transit. The more infrequently used |
| 12 | +\code{UnixStreamServer} and \code{UnixDatagramServer} classes are |
| 13 | +similar, but use Unix domain sockets; they're not available on |
| 14 | +non-Unix platforms. For more details on network programming, consult |
| 15 | +a book such as W. Richard Steven's \emph{Unix Network Programming}_ or |
| 16 | +XXX (a Windows equivalent). |
| 17 | + |
| 18 | +These four classes process requests \dfn{synchronously}; each request |
| 19 | +must be completed before the next request can be started. This isn't |
| 20 | +suitable if each request takes a long time to complete, because it |
| 21 | +requires a lot of computation, or because it returns a lot of data |
| 22 | +which the client is slow to process. The solution is to create a |
| 23 | +separate process or thread to handle each request; the |
| 24 | +\code{ForkingMixIn} and \code{ThreadingMixIn} mix-in classes can be |
| 25 | +used to support asynchronous behaviour. |
| 26 | + |
| 27 | +Creating a server requires several steps. First, you must create a |
| 28 | +request handler class by subclassing the \code{BaseRequestHandler} |
| 29 | +class and overriding its \code{handle()} method; this method will |
| 30 | +process incoming requests. Second, you must instantiate one of the |
| 31 | +server classes, passing it the server's address and the request |
| 32 | +handler class. Finally, call the \code{handle_request()} or |
| 33 | +\code{serve_forever()} method of the server object to process one or |
| 34 | +many requests. |
| 35 | + |
| 36 | +Server classes have the same external methods and attributes, no |
| 37 | +matter 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}{} |
| 44 | +Return an integer file descriptor for the socket on which the server |
| 45 | +is listening. This function is most commonly passed to |
| 46 | +\code{select.select()}, to allow monitoring multiple servers in the |
| 47 | +same process. |
| 48 | +\end{funcdesc} |
| 49 | + |
| 50 | +\begin{funcdesc}{handle_request}{} |
| 51 | +Process a single request. This function calls the following methods |
| 52 | +in order: \code{get_request()}, \code{verify_request()}, and |
| 53 | +\code{process_request()}. If the user-provided \code{handle()} method |
| 54 | +of 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}{} |
| 59 | +Handle 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} |
| 64 | +The 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} |
| 69 | +The user-provided request handler class; an instance of this class is |
| 70 | +created for each request. |
| 71 | +\end{datadesc} |
| 72 | + |
| 73 | +\begin{datadesc}{server_address} |
| 74 | +The address on which the server is listening. The format of addresses |
| 75 | +varies depending on the protocol family; see the documentation for the |
| 76 | +socket module for details. For Internet protocols, this is a tuple |
| 77 | +containing 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} |
| 82 | +The 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 | + |
| 88 | +The server classes support the following class variables: |
| 89 | + |
| 90 | +\begin{datadesc}{request_queue_size} |
| 91 | +The size of the request queue. If it takes a long time to process a |
| 92 | +single request, any requests that arrive while the server is busy are |
| 93 | +placed into a queue, up to \code{request_queue_size} requests. Once |
| 94 | +the queue is full, further requests from clients will get a |
| 95 | +``Connection denied'' error. The default value is usually 5, but this |
| 96 | +can be overridden by subclasses. |
| 97 | +\end{datadesc} |
| 98 | + |
| 99 | +\begin{datadesc}{socket_type} |
| 100 | +The 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 | + |
| 104 | +There are various server methods that can be overridden by subclasses |
| 105 | +of base server classes like \code{TCPServer}; these methods aren't |
| 106 | +useful 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}{} |
| 112 | +Actually processes the request by instantiating |
| 113 | +\code{RequestHandlerClass} and calling its \code{handle()} method. |
| 114 | +\end{funcdesc} |
| 115 | + |
| 116 | +\begin{funcdesc}{get_request}{} |
| 117 | +Must accept a request from the socket, and return a 2-tuple containing |
| 118 | +the \emph{new} socket object to be used to communicate with the |
| 119 | +client, and the client's address. |
| 120 | +\end{funcdesc} |
| 121 | + |
| 122 | +\begin{funcdesc}{handle_error}{request\, client_address} |
| 123 | +This function is called if the \code{RequestHandlerClass}'s |
| 124 | +\code{handle} method raises an exception. The default action is to print |
| 125 | +the traceback to standard output and continue handling further requests. |
| 126 | +\end{funcdesc} |
| 127 | + |
| 128 | +\begin{funcdesc}{process_request}{request\, client_address} |
| 129 | +Calls \code{finish_request()} to create an instance of the |
| 130 | +\code{RequestHandlerClass}. If desired, this function can create a new |
| 131 | +process 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}{} |
| 140 | +Called by the server's constructor to activate the server. |
| 141 | +May be overridden. |
| 142 | +\end{funcdesc} |
| 143 | + |
| 144 | +\begin{funcdesc}{server_bind}{} |
| 145 | +Called by the server's constructor to bind the socket to the desired |
| 146 | +address. May be overridden. |
| 147 | +\end{funcdesc} |
| 148 | + |
| 149 | +\begin{funcdesc}{verify_request}{request\, client_address} |
| 150 | +Must return a Boolean value; if the value is true, the request will be |
| 151 | +processed, and if it's false, the request will be denied. |
| 152 | +This function can be overridden to implement access controls for a server. |
| 153 | +The default implementation always return true. |
| 154 | +\end{funcdesc} |
| 155 | + |
| 156 | +The request handler class must define a new \code{handle} method, and |
| 157 | +can override any of the following methods. A new instance is created |
| 158 | +for each request. |
| 159 | + |
| 160 | +\begin{funcdesc}{finish}{} |
| 161 | +Called after the \code{handle} method to perform any clean-up actions |
| 162 | +required. The default implementation does nothing. If \code{setup()} |
| 163 | +or \code{handle()} raise an exception, this function will not be called. |
| 164 | +\end{funcdesc} |
| 165 | + |
| 166 | +\begin{funcdesc}{handle}{} |
| 167 | +This function must do all the work required to service a request. |
| 168 | +Several instance attributes are available to it; the request is |
| 169 | +available as \code{self.request}; the client address as |
| 170 | +\code{self.client_request}; and the server instance as \code{self.server}, in |
| 171 | +case it needs access to per-server information. |
| 172 | + |
| 173 | +The type of \code{self.request} is different for datagram or stream |
| 174 | +services. For stream services, \code{self.request} is a socket |
| 175 | +object; for datagram services, \code{self.request} is a string. |
| 176 | +However, this can be hidden by using the mix-in request handler |
| 177 | +classes |
| 178 | +\code{StreamRequestHandler} or \code{DatagramRequestHandler}, which |
| 179 | +override the \code{setup} and \code{finish} methods, and provides |
| 180 | +\code{self.rfile} and \code{self.wfile} attributes. \code{self.rfile} |
| 181 | +and \code{self.wfile} can be read or written, respectively, to get the |
| 182 | +request data or return data to the client. |
| 183 | +\end{funcdesc} |
| 184 | + |
| 185 | +\begin{funcdesc}{setup}{} |
| 186 | +Called before the \code{handle} method to perform any initialization |
| 187 | +actions required. The default implementation does nothing. |
| 188 | +\end{funcdesc} |
0 commit comments