visit
Socket programming is a way of making two nodes on a network communicate with each other. One socket listens on a particular port at an IP (server), while another socket reaches out to the other to form a connection (client).
Python socket modules come by default with the Python standard library. Therefore, you don’t need to install anything to get started.Structure of our Project
As we have seen above, sockets are nodes which talk to each other, and in our project, we will implement two nodes using sockets one as client and other as server.
Those two nodes will be sending messages two each other as normal chatting application.Python 3.5.3 (default, Sep 27 2018, 17:25:39)
[GCC 6.3.0 20170516] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import socket
>>> socket.gethostname() #getting our local host name
'kalebu-PC'
>>> socket.gethostbyname('www.google.com') #getting IP of Google
'172.217.170.4'
As we can see in the above example, we were able to get our local host name using gethostname method and also we were able to get the IP address of using gethostbyname method .
Building our First Socket(node)
There are many communication protocols out there depending on where it is being applied, such as Transmission communication Protocol (TCP),
User Datagram Protocol (UDP), File transfer Protocol (FTP), and more.
>>> import socket
>>> node = socket.socket(socket.AF_INET, socket.SOCK_SaTREAM)
Server.py
import socket
import threading
class ServerNode:
def __init__(self):
self.node = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
port_and_ip = ('127.0.0.1', 12345)
self.node.bind(port_and_ip)
self.node.listen(5)
self.connection, addr = self.node.accept()
def send_sms(self, SMS):
self.connection.send(SMS.encode())
def receive_sms(self):
while True:
data = self.connection.recv(1024).decode()
print(data)
def main(self):
while True:
message = input()
self.send_sms(message)
server = ServerNode()
always_receive = threading.Thread(target=server.receive_sms)
always_receive.daemon = True
always_receive.start()
server.main()
Code Explanation
Binding IP address and portport_and_ip = ('127.0.0.1', 12345)
self.node.bind(port_and_ip)
Listening to Incoming connection
self.node.listen(5)
self.connection, addr = self.node.accept()
def send_sms(self, SMS):
self.connection.send(SMS.encode())
def receive_sms(self):
while True:
data = self.connection.recv(1024).decode()
print(data)
def main(self):
while True:
message = input()
self.send_sms(message)
server = ServerNode()
always_receive = threading.Thread(target=server.receive_sms)
always_receive.daemon = True
always_receive.start()
server.main()
Building Client Node
Almost everything on Server node is similar to client node except the fact that client node connect to server instead of listening from connection , therefore big difference is seen on Constructor but Everything is almost the same.Client.py
import socket
import threading
class ClientNode:
def __init__(self):
self.node = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
port_and_ip = ('127.0.0.1', 12345)
self.node.connect(port_and_ip)
def send_sms(self, SMS):
self.node.send(SMS.encode())
def receive_sms(self):
while True:
data = self.node.recv(1024).decode()
print(data)
def main(self):
while True:
message = input()
self.send_sms(message)
Client = ClientNode()
always_receive = threading.Thread(target=Client.receive_sms)
always_receive.daemon = True
always_receive.start()
Client.main()
Previously published at