python – Chat app for local network


Objective: to write an application for exchanging messages between several computers on a local network.

Workbench: Python 3.6

Just starting to deal with network programming and sockets, a mess in my head. At the moment, the only principle of network programming that I have learned is:

  1. A socket is created on the server and waits for client requests
  2. A socket is created on the client and sent to the server
  3. The client receives a response from the server

I cannot understand how the task at hand can be solved. What can be a server in a local network when exchanging messages between nodes? Or is it possible to somehow implement this without a server? Give some guidance.


Implementation without a server is not directly related to working with sockets and will only confuse more during training. Therefore, I will try to explain what is the essence of working with the server. Let's clarify the basic principles a bit.

The application that creates the socket is launched. A socket is defined by two values: host and port .

Host – a value that tells the connections from which interfaces to listen (interface, roughly speaking, a network device) For example, the value says what to listen to on the loopback interface, which is looped on itself and is available only to the device itself, computers join the local network to it will not reach, or is a value that says that you can accept a connection from any interface, such an application can be reached from the device itself, from computers in the local network and even from the global network (if the appropriate settings are made), well, or the IP address inside local network (something in the spirit of ), which will allow access only from computers on the local network.

Port – an identifier that defines the application itself (and also, quite often, the protocol required for communication, 80 – http, 443 – https, 25 – smtp, etc.). It is advisable not to use ports 0 to 1023, as they often have some well-known application and conflicts are possible. 49152-65535 are most often used for short-lived connections, I will mention them a little later. Well, those that remained essentially free for general use.

A socket is created on the client and powered to establish a connection to the server.

It is very important to understand that the client also creates a socket, which is also described by the host and port. Where the host will be the IP address in the subnet through the interface of which the connection will be established, and the port will be randomly generated from the range 49152-65535.

After that, the server will create a link that is described by the 4 values host_servera: port_serverahost client: port_klienta (well, and the file descriptor, but it is not important) It determines who it came from and to whom a request to send a reply. Such a device allows you to communicate with several clients at once.

And now a little about how it should look. Let's agree that HOST is the device's IP address in the subnet, PORT is a port from the valid range.

There is a central application – the server. It is launched on one of the devices on the local network. Listens on or HOST: PORT . It can be accessed from the device itself and from other devices on the local network by knocking only on HOST: PORT .

Scroll to Top