In this article, we are going to discuss the procedure for creating a client-server model in python using sockets. We would be creating a local server in our machine and a client program that will communicate with the server. For the discussed Client-Server model, both the programs (Client and Server) will be running on the same machine.
The Server Side
While creating the server program (server_side.py), the first step is importing the socket module which would allow us to connect and communicate with different nodes on the network.
After importing the socket module, we create the socket by passing two parameters which are socket.AF_INET & socket.SOCK_STREAM. AF_INET is an address family which specifies that our socket will communicate with Internet Protocol v4 addresses (IPv4). SOCK_STREAM indicates that the created socket is of the TCP type. If we want to create an UDP type socket, then we would have to pass SOCK_DGRAM instead.
skt = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
The next step will be to bind the socket. For binding, we need to provide two things, a HOST and a PORT. For HOST, we are using the socket.gethostname command and for PORT, we need to provide a high number as lower valued numbers are already reserved for other well known services. Choosing a high number will ensure that there is no conflict.
After binding the socket, we need to specify how many client connections can be queued before denying anymore. This is done using the listen command.
Using the above code, we have technically created a server program. The next step is listening to clients that may want to connect with the created server. We achieve this by running an endless while loop.
client, address = skt.accept()
info = "u are connected"
Inside an endless while loop, the socket accepts clients that want to connect with us. After accepting the client, we send them a string “u are connected”. However, we cannot directly send a string to the client. We first need to encode the string in 8-bit Unicode Transformation Format and then send it to the client.
The Client Side
Coding a client program is simpler compared to coding the server side.
client = socket.socket()
All we need to do is create the client socket and connect it with the server using the same HOST name and PORT number. In our example, we are receiving what the server is sending using the .recv(1024) function. Here 1024 means that we can read at most 1024 bytes. We also need to decode the received message because we had encoded it in the server side before sending.
Implementing in a Project
In this section, we will implement socket programming knowledge to create a simple GUI application project which will use a client-server model.
AIM: We will create a server program that will contain a dictionary. This dictionary contains items along with the item’s cost. The Client Side will be an User Interface program where the user will provide the item and the quantity. The client will send the user inputs to the server and the server will return the total cost.
Now, obviously we won’t discuss each and every line of the project but we will however, discuss the important bits. The following figure shows the schematic representation of the project.
Once the user input is received by the Client python program, we can start with the socket programming part. But, before we look at the client side code, it will be better if we tackle the server side code first.
The first thing you might notice is that we have imported “pickle”. We need this module because the client side will send a list to the server which will contain the item’s name and the quantity. To transfer something like a list we first need to “pickle” it before sending and then “unpickle” it after receiving.
After the computation is done, we convert the result into a string format, encode it in “utf-8” and send it to the client.
In this article, we discussed how to use sockets to communicate between multiple programs and exchange information between them. For our examples we have used python programming language but the concept of sockets is similar for most of the languages.
The steps for creating a program which implements socket can be broken down as follows.
- Server Side
- Create the Socket
- Bind the Socket by providing the Host and Port
- Put the socket into listening mode by using the listen() function
- Create an endless while loop inside which we accept clients, perform operations and send processed data to the clients
- Close the Client after performing the required tasks
- Client Side
- Create the Socket
- Connect to the server side using Host and Port
- Send data to the server side
- Receive and decode the information sent back by the server
In this particular example, we have used the eel library to connect the client python program to a HTML page. You can refer to the following article for understanding how to use the Python Eel library to create HTML User Interfaces for python programs.