[code.view]

[top] / python / PyMOTW / select / select_echo_slow_client.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
     import time
     
     # Create a TCP/IP socket
     sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
     
     # Connect the socket to the port where the server is listening
     server_address = ('localhost', 10000)
     print >>sys.stderr, 'connecting to %s port %s' % server_address
     sock.connect(server_address)
     
     time.sleep(1)
     
     messages = [ 'Part one of the message.',
                  'Part two of the message.',
                  ]
     amount_expected = len(''.join(messages))
     
     try:
     
         # Send data
         for message in messages:
             print >>sys.stderr, 'sending "%s"' % message
             sock.sendall(message)
             time.sleep(1.5)
     
         # Look for the response
         amount_received = 0
         
         while amount_received < amount_expected:
             data = sock.recv(16)
             amount_received += len(data)
             print >>sys.stderr, 'received "%s"' % data
     
     finally:
         print >>sys.stderr, 'closing socket'
         sock.close()
     

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

contact | logmethods.com