Python is a powerful and versatile programming language that is well-suited to many different tasks, including network programming. In this post, we’ll show you how to use Python to create a socket connection between a server and a client.

Before we do anything I would like to point out that it is recommended to try this on a single machine which acts as both the server and the client. Telusko does this in his tutorial https://www.youtube.com/watch?v=u4kr7EFxAKk and it makes life much simpler as you are not having to fight firewalls and more if you try and connect to a VM in the cloud for example.

With that said, let’s take a look at the server-side code. Here is an example of a simple Python server that listens on a specific port and returns a message to any client that connects to it:

import socket

# Create a socket object
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# Bind the socket to a specific address and port
s.bind(("localhost", 12345))

# Listen for incoming connections
s.listen(5)

while True:
    # Establish a connection with the client
    c, addr = s.accept()
    print("Got connection from", addr)

    # Send a message to the client
    c.send(b"Thank you for connecting")

    # Close the connection
    c.close()

In this example, we first import the socket module, which provides all the necessary functions for working with sockets in Python. Next, we create a new socket object using socket.socket() and specify the address family (AF_INET) and socket type (SOCK_STREAM). These specify IPv4 and TCP respectively. (You can in fact leave these out as they are the default settings.)

We then use the bind() method to bind the socket to a specific address and port. In this case, we’re binding to “localhost” (which refers to the local machine) and port 12345. Don’t use a port in the 1-1000 range as it will probably be busy.

Next, we use the listen() method to start listening for incoming connections. The argument to this method specifies the maximum number of queued connections.

The while loop is used to accept incoming connections, using the accept() method. This method returns a new socket object representing the connection, as well as the address of the client. We use the send() method to send a message to the client, and then close the connection using the close() method.

Now let’s take a look at the client-side code. Here is an example of a Python client that connects to the server and sends a message:

import socket

# Create a socket object
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# Connect to the server
client_socket.connect(('localhost', 1234))

# Receive data from the server
data = client_socket.recv(1024)
print(f"Received data: {data}")

# Close the client socket
client_socket.close()

This client code is similar to the server code, but with some important differences. First, instead of calling bind() and listen(), we call connect() to connect to the server. We then use the send() method to send a message to the server, and the recv() method to receive data from the server. Finally, we close the connection using the close() method.