Socket Programming in Python

Socket programming is a way of connecting two nodes on a network to communicate with each other. One socket(node) listens on a particular port at an IP, while the other socket reaches out to the other to form a connection. The server forms the listener socket while the client reaches out to the server.

Socket Programming Digram :

1:- Server

# first of all import the socket library
import socket
# next create a socket object
socket_obj = socket.socket()
# Next bind to the port
socket_obj.bind(('localhost', 8002))
# put the socket into listening mode
# Establish connection with client.
client_obj,address= socket_obj.accept()
# receive data from client
recv_msg = client_obj.recv(1024)
# Decode data
# Print Data
# Close the connection with the client

1:- Client

# import the socket library
import socket
# Create a socket object
socket_obj = socket.socket()
# connect to the server using host and port
socket_obj.connect(('localhost', 8002))
# input data
msg = str(input("Entter your message : "))
# encode and send the data from input data

Here is the final result :


View Github