[code.view]

[top] / python / PyMOTW / select / select_echo_multiclient.py

     #!/usr/bin/env python
     # encoding: utf-8
     #
     # Copyright (c) 2010 Doug Hellmann.  All rights reserved.
     #
     """Client half of echo example
     """
     #end_pymotw_header
     
     import socket
     import sys
     
     messages = [ 'This is the message. ',
                  'It will be sent ',
                  'in parts.',
                  ]
     server_address = ('localhost', 10000)
     
     # Create a TCP/IP socket
     socks = [ socket.socket(socket.AF_INET, socket.SOCK_STREAM),
               socket.socket(socket.AF_INET, socket.SOCK_STREAM),
               ]
     
     # Connect the socket to the port where the server is listening
     print >>sys.stderr, 'connecting to %s port %s' % server_address
     for s in socks:
         s.connect(server_address)
     
     for message in messages:
     
         # Send messages on both sockets
         for s in socks:
             print >>sys.stderr, '%s: sending "%s"' % (s.getsockname(), message)
             s.send(message)
     
         # Read responses on both sockets
         for s in socks:
             data = s.recv(1024)
             print >>sys.stderr, '%s: received "%s"' % (s.getsockname(), data)
             if not data:
                 print >>sys.stderr, 'closing socket', s.getsockname()
                 s.close()
     

[top] / python / PyMOTW / select / select_echo_multiclient.py

contact | logmethods.com