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