Build a chat tool with Python (Part 1): Socket

Tram Ho

  • Python is a high-level programming language for versatile programming purposes, created by Guido van Rossum and first launched in 1991.
  • Currently, Python has become one of the most popular languages ​​in the world today thanks to its strong advantages:
    • Easy to read, easy to learn and easy to remember.
    • There are very bright forms, clear structure, convenient for beginners to learn programming
    • The structure of Python also allows users to write code with minimal keystrokes
    • Can be applied in many fields such as: Web Development, Data Science, Scripting, …..
  • With the above advantages, what crime do we not try to learn Python? I’ve been tinkering with Python for the last time, and today decided to share a little bit of the knowledge I learned for everyone to refer to.
  • Regarding the basic Python syntax, I would like to not repeat above. People can learn on W3Schools , very complete, with examples and exercises for you to practice directly for each part for you.
  • In order for you to understand thoroughly, I would like to divide this article into 3 parts.
    • Part 1 – (Ie this article): Introduction to Socket, Socket programming in Python.
    • Part 2: Learn about multithreading in Python
    • Part 3: Building basic chat tool with python using Socket, multithreading.
  • Within the scope of this article, I will introduce to you about Socket, using Socket to connect Client – Server in Python

Introduction to Socket

  • A socket is an end-point endpoint in a two-way communication link that represents the connection between the Client – Server.
  • Socket is used to send data back and forth between Client and Server via network (network)
  • The network can be logical, local area network to the computer or a physical connection to the external network, with their own connection to other networks. The best example is the Internet, which you connect via ISP (Internet Service Provider).

Socket classification

  • Socket is divided into 2 main types:
    • Stream Socket
    • Datagram Socket

    Stream Socket

  • Stream Socket is based on TCP protocol and data transfer only takes place between two established connection processes. This protocol ensures data is transmitted to the recipient reliably, in the right order thanks to the network traffic management mechanism and anti-congestion mechanism.
  • What are the advantages of Stream Socket?
    • The transmitted data is guaranteed to arrive in the right place and in the right order.
    • Each message is sent with a confirmation to notify the user of the transmission process.
  • Defect:
    • There is only 1 IP between server and receiver, so when connecting, 1 machine must wait for the other device to accept the connection.
    • Speed ​​is not fast
  • It is suitable for applications that require a high level of accuracy, without a high speed requirement. We can mention a few applications such as Mail, bank payment, …

    Datagram Socket

  • Based on the UDP protocol, data transmission does not require a connection between the two processes.
  • What are the advantages of Datagram Socket?
    • The process of connecting and transmitting information is simple, without performing many operations.
    • Extremely fast data transmission time.
  • Defect
    • The process of transmitting information is not reliable, information can be transmitted in the wrong order or be duplicated.
  • Suitable for applications such as live streams, video calls, … because these applications require fast speed, if a few packets lost during transmission, it will not affect too much.

Build a TCP Socket connection with Python

  • In this section, I will guide you to set up a TCP Socket connection between a Client and a Server.
  • The server will be responsible for sending back the data that the client sent.

Server setup

  • Here I have file containing Server code:

  • Do not worry if you read and do not understand anything. Because I will explain each part right below.

Open 1 Server to listen

  • Python provides you with socket modules to work with Socket, so you need to import them to use.

  • Host is the variable that stores the server address, while PORT is the gateway address. On the Client side will also have to have these 2 parameters to be able to connect to the Server
  • The socket.socket() function initializes a socket object to execute.
  • When you use it inside a with function, if any error occurs, it will automatically close the connection. To understand more about the with function you can read here
  • The above code will be equivalent to:

  • Try running the above code and you will get the following:
  • If the above results, you have created a server to listen already.
  • The listen() function has a backlock parameter. It allows you to specify the number of allowed connections to your server. If not passed, it will get the default backlock value.

Communicate with Client

  • When a client sends a request for connection, the accept () function accepts the connection from the Client, allowing the Client to send data to and process it.
  • The while True function while True server to continue processing the incoming Client data until the Client disconnects.
  • The code in the while function is the handle of the Server to the data from the Client. As above, the server sends back any data the client sends.

Client settings

  • Create a file as follows:

  • HOST and PORT here are the address and the port that connects to the server. The client and server must have these two parameters in order to connect.
  • The connect function sends a request to connect to the Server. If the server approves, the client will be able to communicate with the server.
  • Here I send the text Hello, world , Server will receive and send back, Client print to the screen.


  • OK now it’s time to test the results.
  • B1: First open the terminal, and run the file
  • : Open 1 more terminal tab, run file
  • B3: See results
    • Server side:

    • Client side:

  • If you get the above results, you are already successful.


  • So I have finished sharing with everyone the basic knowledge about Socket, and how to build a TCP Client – Server connection.
  • In the next section, I will introduce to you one more knowledge that is quite impressive, but it is indispensable to build a tool chat: Multiple Threads.
  • Link to the next section here.


Share the news now

Source : Viblo