Learning Python Network Programming

(Sean Pound) #1

Client and Server Applications


A multithreaded echo server


A benefit of the multithreading approach is that the OS handles the thread switches
for us, which means we can continue to write our program in a procedural style.
Hence we only need to make small adjustments to our server program to make it
multithreaded, and thus, capable of handling multiple clients simultaneously.


Create a new file called 1.3-echo_server-multi.py and add the following code
to it:


import threading
import tincanchat

HOST = tincanchat.HOST
PORT = tincanchat.PORT

def handle_client(sock, addr):
""" Receive one message and echo it back to client, then close
socket """
try:
msg = tincanchat.recv_msg(sock) # blocks until received
# complete message
msg = '{}: {}'.format(addr, msg)
print(msg)
tincanchat.send_msg(sock, msg) # blocks until sent
except (ConnectionError, BrokenPipeError):
print('Socket error')
finally:
print('Closed connection to {}'.format(addr))
sock.close()

if __name__ == '__main__':
listen_sock = tincanchat.create_listen_socket(HOST, PORT)
addr = listen_sock.getsockname()
print('Listening on {}'.format(addr))

while True:
client_sock,addr = listen_sock.accept()
# Thread will run function handle_client() autonomously
# and concurrently to this while loop
thread = threading.Thread(target=handle_client,
args=[client_sock, addr],
daemon=True)
thread.start()
print('Connection from {}'.format(addr))
Free download pdf