[code.view]

[top] / python / PyMOTW / SocketServer / SocketServer_echo.py

     #!/usr/bin/env python
     #
     # Copyright 2007 Doug Hellmann.
     #
     #
     #                         All Rights Reserved
     #
     # Permission to use, copy, modify, and distribute this software and
     # its documentation for any purpose and without fee is hereby
     # granted, provided that the above copyright notice appear in all
     # copies and that both that copyright notice and this permission
     # notice appear in supporting documentation, and that the name of Doug
     # Hellmann not be used in advertising or publicity pertaining to
     # distribution of the software without specific, written prior
     # permission.
     #
     # DOUG HELLMANN DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
     # INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN
     # NO EVENT SHALL DOUG HELLMANN BE LIABLE FOR ANY SPECIAL, INDIRECT OR
     # CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
     # OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
     # NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
     # CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     #
     
     """Echo server example for SocketServer
     
     """
     
     __version__ = "$Id$"
     #end_pymotw_header
     
     import logging
     import sys
     import SocketServer
     
     logging.basicConfig(level=logging.DEBUG,
                         format='%(name)s: %(message)s',
                         )
     
     class EchoRequestHandler(SocketServer.BaseRequestHandler):
         
         def __init__(self, request, client_address, server):
             self.logger = logging.getLogger('EchoRequestHandler')
             self.logger.debug('__init__')
             SocketServer.BaseRequestHandler.__init__(self, request, client_address, server)
             return
     
         def setup(self):
             self.logger.debug('setup')
             return SocketServer.BaseRequestHandler.setup(self)
     
         def handle(self):
             self.logger.debug('handle')
     
             # Echo the back to the client
             data = self.request.recv(1024)
             self.logger.debug('recv()->"%s"', data)
             self.request.send(data)
             return
     
         def finish(self):
             self.logger.debug('finish')
             return SocketServer.BaseRequestHandler.finish(self)
     
     class EchoServer(SocketServer.TCPServer):
         
         def __init__(self, server_address, handler_class=EchoRequestHandler):
             self.logger = logging.getLogger('EchoServer')
             self.logger.debug('__init__')
             SocketServer.TCPServer.__init__(self, server_address, handler_class)
             return
     
         def server_activate(self):
             self.logger.debug('server_activate')
             SocketServer.TCPServer.server_activate(self)
             return
     
         def serve_forever(self):
             self.logger.debug('waiting for request')
             self.logger.info('Handling requests, press <Ctrl-C> to quit')
             while True:
                 self.handle_request()
             return
     
         def handle_request(self):
             self.logger.debug('handle_request')
             return SocketServer.TCPServer.handle_request(self)
     
         def verify_request(self, request, client_address):
             self.logger.debug('verify_request(%s, %s)', request, client_address)
             return SocketServer.TCPServer.verify_request(self, request, client_address)
     
         def process_request(self, request, client_address):
             self.logger.debug('process_request(%s, %s)', request, client_address)
             return SocketServer.TCPServer.process_request(self, request, client_address)
     
         def server_close(self):
             self.logger.debug('server_close')
             return SocketServer.TCPServer.server_close(self)
     
         def finish_request(self, request, client_address):
             self.logger.debug('finish_request(%s, %s)', request, client_address)
             return SocketServer.TCPServer.finish_request(self, request, client_address)
     
         def close_request(self, request_address):
             self.logger.debug('close_request(%s)', request_address)
             return SocketServer.TCPServer.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.server_address # find out what port we were given
     
         t = threading.Thread(target=server.serve_forever)
         t.setDaemon(True) # don't hang on exit
         t.start()
     
         logger = logging.getLogger('client')
         logger.info('Server on %s:%s', ip, port)
     
         # Connect to the server
         logger.debug('creating socket')
         s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
         logger.debug('connecting to server')
         s.connect((ip, port))
     
         # Send the data
         message = 'Hello, world'
         logger.debug('sending data: "%s"', message)
         len_sent = s.send(message)
     
         # Receive a response
         logger.debug('waiting for response')
         response = s.recv(len_sent)
         logger.debug('response from server: "%s"', response)
     
         # Clean up
         logger.debug('closing socket')
         s.close()
         logger.debug('done')
         server.socket.close()
     

[top] / python / PyMOTW / SocketServer / SocketServer_echo.py

contact | logmethods.com