In general terms, sockets are interior endpoints built for sending and receiving data. A single network will have two sockets, one for each communicating device or program. These sockets are a combination of an IP address and a Port. A single device can have ‘n’ number of sockets based on the port number that is being used. Different ports are available for different types of protocols.
Socket programming is one of the most fundamental technologies of computer network programming . A socket is an endpoint of a two-way communication link between two programs running on the network. The client and server can communicate by writing to or reading from their sockets. Python has quite an easy way to start with the socket interface. Pythons’s socket module provides access to the BSD socket interface . It is available on all modern Unix systems, Windows, Mac OS X, BeOS, OS/2, and probably additional platforms.
The Python Socket Programming has two sections:
•Python Server Socket Program
•Python Client Socket Program
Python Server Socket Program
The Server Socket Program here is a Python Console based Application . This program act as a Server and listening to clients request from Port No. 8080.
server.bind((LOCALHOST, PORT)) server.listen(1)
When the Server Socket accept a request from the Client side, it reads the data from client and also it write the response to the connected client program .
Python Server Socket Example (Server . py) import socket
LOCALHOST = “127.0.0.1”
PORT = 8080
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server.bind((LOCALHOST, PORT))
print(“Waiting for client request..”)
clientConnection,clientAddress = server.accept() print(“Connected clinet :” , clientAddress)
data = clientConnection.recv(1024)
print(“From Client :” , data.decode()) clientConnection.send(bytes(“Successfully Connected to Server!!”,’UTF-8′)) clientConnection.close()
Python Client Socket Program
The Client socket is connected to the Port 8080 of the Python Server Socket Program , and the IP Address (“127.0.0.1”) of the server machine. Here we give as 127.0.0.1 , because the Server and Client running on the same machine. If the client program running on other machine, then you can give the IP Address of that machine.
When the Python Client program starts , it will connect to the Python Server Socket Program and waiting input from client side. When you type the message it will send to the server and then you can see the reply messages from server side too
Python Socket Client Example(client . py)
SERVER = “127.0.0.1”
PORT = 8080
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) client.connect((SERVER, PORT))
client.sendall(bytes(“This is from Client”,’UTF-8′))
data = client.recv(1024)
How to run this program ?
Create Python Server Socket Program (Server . py) and Python Client Socket Program (client . py) in two separate files. When you finish coding, first you have to start Python Server Socket Program from DOS prompt (console) , then you will get a message ” Server Started…” and “Waiting for client request..” in your DOS screen, where the server program is running .
Next step is to start Python Client Socket Program from DOS prompt (console) in the same computer or other computers on the same network . When you start the client program , it will establish a
connection to the Server and send message (“This is from Client”) from client side. After receiving message from client side, the server send message to the client “Successfully Connected to Server!!”. That’s…now you can see you client program and server program communicate each other.
Why Use Python Sockets to Send Data?
Internet-connected applications that need to operate in real time greatly benefit from the implementation of sockets in their networking code. Some examples of apps that use socket programming are:
•Web pages that show live notifications (Facebook, Twitch, eBay)
•Multiplayer online games (League of Legends, WoW, Counter Strike)
•Chat apps (WhatsApp, WeChat, Slack)
•Real-time data dashboards (Robinhood, Coinbase)
•IoT devices (Nest, August Locks)
With streaming sockets, data can be sent or received at any time. In case your Python program is in the middle of executing some code, other threads can handle the new socket data. Libraries like asyncio implement multiple threads, so your Python program can work in an asynchronous fashion.
The socket Module
The socket.socket() function is used for creating a socket that is available in the socket module. Here’s a general syntax is given below:
s = socket.socket (socket_family, socket_type, protocol=0)
In the above syntax, we have used socket_family as the domain such as AF_INET or AF_UNIX, as discussed earlier. Next, we have used socket_type that can be either SOCK_DGRAM or SOCK_STREAM. At last, we have a protocol that is usually optional and zero (0) by default.
Once we have created the socket object, we would be using some functions to create our client or server program.
Socket programming is a way of connecting two nodes on a network to communicate with each other. One node(socket) listens on a particular Port at an IP and another node(socket) reaches out to it. The Server is a listener Socket and the client reaches out to the server. In simple terms, these are nothing but a Server and Client. Sockets are the real backbones behind web browsing.