Table of Contents
A Python server and client socket connection is a two-way communication link that are running on a network. This is known as a network socket and can be on the same system or different systems that are connected to a network. Both the server and client communicate with each other by reading or writing from the network socket. This is very similar to a communicate between two people via a telephone and the network socket represents the number of the telephone number of a mobile phone.
Python Sockets and Sending Data
Sockets are great for sending data and many applications that are internet-connected that operate in real-time benefit from sockets and networking code. Many application use sockets in real-time such as Facebook, eBay, Twitch, and WhatsApp.
Server Connection with Python Sockets
The Python socket library offers an internet streaming socket by using the socket.socket function with the parameters socket.AF_INET and socket.SOCK_STREAM. The method socket.socket() create a TCP/IP socket with the use of socket.AF_INET and socket.SOCK_STREAM. In addition, this library will allow the retrieval of the fully qualified domain name (getfqdn), hostname (gethostname), and the IP address (gethostbyname). Also, by creating and connecting the socket, the socket will be bound to the IP address with a port number 23456 by using the bind() method.
The server must listen for any incoming connections that specify the same port and the listen() method will be used. While the server waits for an incoming connection, a while loop will be used for the incoming requests and the accept() method will be accept the requests. The recv() method is used to read the data submitted and output the data with stdout. Finally, the connection will be closed once no data is remaining from the client.
Client Connection with Python Sockets
The client code will be very similar to the server code. The server side code we used a while loop, but the client side code will use a for loop that uses a connect() method. The method socket.sendall() allows data to be sent to a server where the socket is connected and the client can receive data with this function. We will provide data to the server side and upon the for loop reading the data and all data is sent, the close() method will close the connection to the server side.
Server Side Socket Connection Code
import socket import time import sys sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) local_hostname = socket.gethostname() local_fqdn = socket.getfqdn() ip_address = socket.gethostbyname(local_hostname) port = 55468 server_address = (ip_address, port) #print (f"Working on {local_hostname} ({local_fqdn}) with {ip_address}") time.sleep(1) print ("Starting server-side connection: ") #print (f"\tIP Address {ip_address} ") #print (f"\tPort: {port} ") time.sleep(1) sock.bind(server_address) sock.listen(1) timeout = 0 def acceptConnection(): while True: print("Waiting for a connection from client-side.") connection, client_address = sock.accept() try: print("Received connection from client.") #print ('Connection from', client_address) while True: data = connection.recv(64) if data: print (f"Received data: {data} ") else: print ("\nThere is no more data to receive.") break finally: connection.close() print("nConnection closed.") exit() acceptConnection()
Client Side Socket Connection Code
import socket import time def clientSideConnection(): sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) local_hostname = socket.gethostname() local_fqdn = socket.getfqdn() ip_address = socket.gethostbyname(local_hostname) port = 55468 server_address = (ip_address, port) sock.connect(server_address) #print (f"Connecting to {local_hostname} ({local_fqdn}) with {ip_address}") print ("Connected to server-side. ") time.sleep(2) call_data = ["2:30pm", "3:45pm", "3:56pm", "8:15pm", "9:21pm", "11:23pm"] for entry in call_data: print (f"Call Time: {entry} ") new_data = str(f"Call Time: {entry}").encode("utf-8") sock.sendall(new_data) time.sleep(2) sock.close() clientSideConnection()
Python Server and Client Connection Execution
In order to execute the above, launch two command (cmd) prompts in Windows. Locate your path where the scripts are saved and execute the server side script first. After you see,’ Waiting for a connection’, execute the client side connection as shown below.
Server Side Execution in Python
Client Side Execution in Python