Denizhalil

Python Socket Programming: Building a Server and Client

Socket programming is a powerful tool for facilitating data communication over a network, and Python provides an excellent environment for building socket-based applications. In this tutorial, we will walk you through the steps to create a server and client application using Python.

Understanding Servers and Clients

  • Server: A server is a program that listens for and handles incoming requests from clients. Servers typically run continuously to provide services to clients.
  • Client: Clients are programs that connect to servers and request services or data.

Prerequisites

To create a server, follow these steps:

  1. Import the socket module.
  2. Create a server socket and specify an IP address and port number.
  3. Bind the server socket to the specified IP address and port.
  4. Start listening for incoming connections and accept them.
  5. Receive and process data from the client.
  6. Close the connection and the server socket.

Here is a Python code example that demonstrates these steps:

import socket

# Create a server socket
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# Specify an IP address and port
server_ip = '127.0.0.1'  # localhost
server_port = 12345

# Bind the server socket to the IP address and port
server_socket.bind((server_ip, server_port))

# Start listening for incoming connections (accept up to 5)
server_socket.listen(5)
print(f"Server is listening on {server_ip}:{server_port}...")

# Accept incoming connections from clients
client_socket, client_address = server_socket.accept()
print(f"Accepted a connection from {client_address}.")

# Receive data from the client and print it
while True:
    data = client_socket.recv(1024).decode()
    if not data:
        break
    print(f"Received data: {data}")

# Close the connection
client_socket.close()
server_socket.close()

This code creates a server socket, binds it to the specified IP address and port, accepts incoming connections, and processes received data.
Not: Don’t forget to read our article on creating a web server with Python

Step 2: Creating the Client

To create a client, follow these steps:

  1. Import the socket module.
  2. Create a client socket and specify the server’s IP address and port.
  3. Connect to the server.
  4. Send data to the server.
  5. Close the connection.

Here is a Python code example for the client:

import socket

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

# Specify the server's IP address and port
server_ip = '127.0.0.1'  # Server's IP address
server_port = 12345

# Connect to the server
client_socket.connect((server_ip, server_port))
print(f"Connected to the server at {server_ip}:{server_port}.")

while True:
    message = input("Enter the message to send ('q' to quit): ")
    if message == 'q':
        break
    # Send the data
    client_socket.send(message.encode())

# Close the connection
client_socket.close()

This code creates a client socket, connects to the server, sends user input to the server, and closes the connection.
Not: You can check out nearly 40 Python projects on my github address. 😉

Conclusion

In this article, we explored how to create a simple server and client application using Python. Socket programming is a powerful technique for network communication, and this basic example serves as a starting point for more complex networking applications. With a solid understanding of these fundamentals, you can confidently move on to building more advanced networked systems.

Leave a Comment

Join our Mailing list!

Get all latest news, exclusive deals and academy updates.