In this case, no special server class is required since the recv ( len_sent ) print 'Received: " %s "' % response # Clean up s. send ( message ) # Receive a response response = s. connect (( ip, port )) # Send the data message = 'Hello, world' print 'Sending : " %s "' % message len_sent = s. start () # Connect to the server s = socket. setDaemon ( True ) # don't hang on exit t. server_address # find out what port we were given t = threading. TCPServer ( address, EchoRequestHandler ) ip, port = server. send ( data ) return if _name_ = '_main_' : import socket import threading address = ( 'localhost', 0 ) # let the kernel give us a port server = SocketServer. BaseRequestHandler ): def handle ( self ): # Echo the back to the client data = self. Import SocketServer class EchoRequestHandler ( SocketServer. Here is a simpler version of the same thing, without the Listen on a specific port each time you run it, provide that number in The port number used will change each time you run it, as the kernelĪllocates an available port automatically. The output for the program should look something like this:ĮchoServer: Handling requests, press to quitĮchoServer: verify_request(, ('127.0.0.1', 56211))ĮchoServer: process_request(, ('127.0.0.1', 56211))ĮchoServer: finish_request(, ('127.0.0.1', 56211))ĮchoRequestHandler: recv()->"Hello, world"Ĭlient: response from server: "Hello, world" debug ( 'response from server: " %s "', response ) # Clean up logger. debug ( 'waiting for response' ) response = s. send ( message ) # Receive a response logger. debug ( 'sending data: " %s "', message ) len_sent = s. connect (( ip, port )) # Send the data message = 'Hello, world' logger. info ( 'Server on %s : %s ', ip, port ) # Connect to the server logger. close_request ( self, request_address ) if _name_ = '_main_' : import socket import threading address = ( 'localhost', 0 ) # let the kernel give us a port server = EchoServer ( address, EchoRequestHandler ) ip, port = server. debug ( 'close_request( %s )', request_address ) return SocketServer. finish_request ( self, request, client_address ) def close_request ( self, request_address ): self. debug ( 'finish_request( %s, %s )', request, client_address ) return SocketServer. server_close ( self ) def finish_request ( self, request, client_address ): self. debug ( 'server_close' ) return SocketServer. process_request ( self, request, client_address ) def server_close ( self ): self. debug ( 'process_request( %s, %s )', request, client_address ) return SocketServer. verify_request ( self, request, client_address ) def process_request ( self, request, client_address ): self. debug ( 'verify_request( %s, %s )', request, client_address ) return SocketServer. handle_request ( self ) def verify_request ( self, request, client_address ): self. debug ( 'handle_request' ) return SocketServer. handle_request () return def handle_request ( self ): self. info ( 'Handling requests, press to quit' ) while True : self. server_activate ( self ) return def serve_forever ( self ): self. debug ( 'server_activate' ) SocketServer. _init_ ( self, server_address, handler_class ) return def server_activate ( self ): self. TCPServer ): def _init_ ( self, server_address, handler_class = EchoRequestHandler ): self.
finish ( self ) class EchoServer ( SocketServer. send ( data ) return def finish ( self ): self. debug ( 'handle' ) # Echo the back to the client data = self. setup ( self ) def handle ( self ): self. _init_ ( self, request, client_address, server ) return def setup ( self ): self. BaseRequestHandler ): def _init_ ( self, request, client_address, server ): self. DEBUG, format = ' %(name)s : %(message)s ', ) class EchoRequestHandler ( SocketServer. Import logging import sys import SocketServer logging. Modifications, and provide a request handler class for it to work with This division of responsibility means that in many cases youĬan simply use one of the existing server classes without any (interpreting incoming data, processing it, sending data back to theĬlient). The server deals with theĬommunication issues (listing on a socket, accepting connections,Įtc.) and the request handler deals with the “protocol” issues Responsibility for processing a request is split between a serverĬlass and a request handler class. Or process for each request, depending on what is most appropriate for Mix-in classes for easily converting servers to use a separate thread Over TCP, UDP, Unix streams, and Unix datagrams. (the server request handler blocks until the request is completed) It defines classes for handling synchronous network requests The SocketServer module is a framework for creating network