Socket programming in Python
Ever thought about how an online multiplayer game works. How while playing a game, your friend knocks you out with a grenade and it’s reflected on every player’s screen. A very basic way to explain all this is through a client-server model connected via a socket. In this article, I will explain how a client-server model works and how you can create one using very basic python programming.
Sockets work on this client-server model that has their central servers and the clients all around the world where you are the client playing the game. Now all the clients connect to the server which in turn communicates to each client. Every action performed by a particular client is sent to the server which in turn sends that action to the rest of the clients connected to the server. Now obviously this way is much better and quicker than connecting each client to all other clients.
Few elemental concepts
What is IP address and how does your device access content from the internet?
So basically, your modem is given a unique public IP address (more precisely it’s there on the wan interface of the router) that represents your physical location on the internet through which the modem communicates to the internet.
Each device on your local network is given an IP address(called the IPV4 address) through which the router communicates with each device on the network.
So roughly, this is how it works every time you search something on the internet, your device pings the router then the router pings the modem which in turn pings the internet, and after getting the required information from the internet same path is traced back to your device.
Diving into the code
SERVER-SIDE SCRIPTING
Here first of all we assign our local IPV4 address and choose an available port on your system. Then create your socket using the socket library available in python and bind the socket to the address of the system and port so that anything that makes a connection to the above services will be directed to this socket.
Pro Tip:
You can either hardcode your local IPV-4 address just go to your command prompt and type ipconfig to get your IPV4 address or a better way would be to use:
Get Lokesh Mishra’s stories in your inbox
Join Medium for free to get updates from this writer.
SERVER = socket.gethostbyname(socket.gethostname())
which automatically gets the IPV4 address of your system and you won’t have to hardcode it every time you switch your system.
Now we create a function which will start the server and make each client available in a separate thread using the concept of multithreading this and put all of the message handling stuff inside separate threads for each client that connects to the server so that one client is not waiting for another client to send or receive a message. In simple words, multiple clients can access the server at the same time.
Thereafter for each client, we first create a connection, then receive the message sent by the client and display it, and at last close the connection so that it is available anytime in the future. It’s that simple.
CLIENT-SIDE SCRIPTING
The initial portion is similar to the server-side code where a connection of the server is made to the socket.
Now we create a function where the message entered by the user is first encrypted in byte format whereafter it is sent to the server which is waiting for the client’s message.
This is how the output will look like
First, you need to start the server so that it’s waiting for any client who connects to the same socket, then run the client.py file and enter any text you want to be received on the server. That’s it you have created your client-server model.
Now to implement this concept other than on your local machine only is to simply replace the IPv4 address entered in the SERVER = “ ” with your PUBLIC IP ADDRESS after which any client using the same script can connect to the server running on your system from anywhere around the world.








