A chat program

In the previous section, we went through the basics of creating a socket server and client in Python. In this section, we will write a chat application in Python that is powered by Python sockets.

The chat application we are going to make will be a common chat room rather than a peer-to-peer chat. So this means that multiple chat users can connect to the chat server and exchange messages. Every message is broadcast to every connected chat user.

The chat server

The chat server performs the following tasks:

  • It accepts multiple incoming connections for the client.
  • It reads incoming messages from each client and broadcasts them to all the other connected clients.

The following is the code for the chat server:

import socket, select #Function ...

Get Raspberry Pi: Amazing Projects from Scratch now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.