Python Program that Sends And Receives Message from Client Last Updated : 06 Sep, 2024 Comments Improve Suggest changes Like Article Like Report 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 is started by importing the socket library and making a simple socket. import sockets = socket.socket(socket.AF_INET, socket.SOCK_STREAM)Here we made a socket instance and passed it two parameters. The first parameter is AF_INET and the second one is SOCK_STREAM. AF_INET refers to the address family ipv4. The SOCK_STREAM means connection-oriented TCP protocol.Note: For more information, refer to Socket Programming in PythonNow we can connect to a server using Server:A Server is a program that provides service to other computers on the network or Internet. Similarly, a client is a program that receives services from the server. When a server wants to communicate with a client, there is a need for a socket. A socket is a point of connection between the server and the client.TCP/IP server program that sends message to the client. Python3 import socket # take the server name and port name host = 'local host' port = 5000 # create a socket at server side # using TCP / IP protocol s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # bind the socket with server # and port number s.bind(('', port)) # allow maximum 1 connection to # the socket s.listen(1) # wait till a client accept # connection c, addr = s.accept() # display client address print("CONNECTION FROM:", str(addr)) # send message to the client after # encoding into binary string c.send(b"HELLO, How are you ? \ Welcome to Akash hacking World") msg = "Bye.............." c.send(msg.encode()) # disconnect the server c.close() TCP/IP server program that receive message from server. Python3 import socket # take the server name and port name host = 'local host' port = 5000 # create a socket at client side # using TCP / IP protocol s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # connect it to server and port # number on local computer. s.connect(('127.0.0.1', port)) # receive message string from # server, at a time 1024 B msg = s.recv(1024) # repeat as long as message # string are not empty while msg: print('Received:' + msg.decode()) msg = s.recv(1024) # disconnect the client s.close() Note: Open In Two Separate DOS Windows And First Execute server, then Execute client.Output of Server: Output of Client: Comment More infoAdvertise with us Next Article Python Program that Sends And Receives Message from Client akash22675 Follow Improve Article Tags : Python Programming Language Python Programs Python-socket Practice Tags : python Similar Reads Telnet - Python Network programming Telnet is a networking protocol that follows a client-server model. It uses TCP as its underlying communication protocol. It is typically used to start and a remote command-line session, typically on a server. Some facts about telnet:Uses Transmission Control Protocol for data transmission.Bi-direct 5 min read Making a Port-Scanner in Kali Linux Terminal Using Python In computer networking, a port is a virtual point where network connections start and end. It's like an open door of your home, If you don't close this then anyone can Enter your home. A port scanner is a program that is searching ports in a network and tries to find which ports are virtually open a 3 min read Python Network Programming Python provides two levels of access to network programming. These are -Â Low-Level Access: At the low level, you can access the basic socket support of the operating system. You can implement client and server for both connection-oriented and connectionless protocols.High-Level Access: At the high l 5 min read Send an SMS Message with Python In today's fastest-growing world, SMS is still a powerful tool by which we can reach billions of users and one can establish a connection globally. In this new world when instant messaging and social media are dominating you can feel our humble SMS outdated but you don't underestimate its power, it 4 min read Telnet with ipaddress and port in Python Telnet is a protocol that allows you to connect to a remote computer and execute commands. Python provides a built-in module called "telnetlib" that enables us to use Telnet in our code. What is Telnet in Python?Telnet clients let you connect to other Telnet Servers. You cannot connect to your own s 3 min read Like