# Source: F. Sarker, S. Washington. "Learning Python Network Programming" import tincanchat HOST = '127.0.0.1' #tincanchat.HOST PORT = tincanchat.PORT def handle_client(sock, addr): """Receive data from the client via sock and echo it back""" try: # Blocks until received complete message msg = tincanchat.recv_msg(sock) print('{0}: {1}'.format(addr, msg)) # Blocks until sent tincanchat.send_msg(sock, msg) except (ConnectionError, BrokenPipeError): print('Socket error') finally: print('Closed connection to {0}'.format(addr)) sock.close() listen_sock = tincanchat.create_listen_socket(HOST, PORT) addr = listen_sock.getsockname() print('Listening on {0}'.format(addr)) while True: client_sock, addr = listen_sock.accept() print('Connection from {0}'. format(addr)) handle_client(client_sock, addr)