Tutorial

Python Socket Programming: Server and Client Example Guide

Updated on February 21, 2025
Python Socket Programming: Server and Client Example Guide

Introduction

Python’s socket module is a powerful tool for creating network applications. In this tutorial, you will learn the basics of Python socket programming, including how to create a simple client-server architecture, handle multiple clients using threading, and understand the differences between TCP and UDP sockets. You will learn how to establish connections, send and receive data, and build robust network applications using Python.

What is Socket Programming in Python?

To understand Python socket programming, we need to know about three interesting topics - Socket Server, Socket Client, and Socket.

So, what is a server? Well, a server is software that waits for client requests and serves or processes them accordingly.

On the other hand, a client is a requester of this service. A client program requests some resources from the server, and the server responds to that request.

The socket is the endpoint of a bidirectional communications channel between the server and the client. Sockets may communicate within a process, between processes on the same machine, or between processes on different machines. For any communication with a remote program, we have to connect through a socket port. The main objective of this socket programming tutorial is to familiarize you with how socket servers and clients communicate with each other. You will also learn how to write a Python socket server program.

Python Socket Example

We have said earlier that a socket client requests for some resources to the socket server and the server responds to that request. So we will design both server and client model so that each can communicate with them. The steps can be considered like this.

  1. Python socket server program executes at first and waits for any request
  2. Python socket client program will initiate the conversation at first.
  3. Then server program will response accordingly to client requests.
  4. Client program will terminate if user enters “bye” message. Server program will also terminate when client program terminates, this is optional and we can keep server program running indefinitely or terminate with some specific command in client request.

Python Socket Server

We will save the Python socket server program as socket_server.py. To use python socket connection, we need to import socket module. Then, sequentially we need to perform some task to establish connection between server and client. We can obtain host address by using socket.gethostname() function. It is recommended to user port address above 1024 because port number lesser than 1024 are reserved for standard internet protocol. See the below python socket server example code, the comments will help you to understand the code.

import socket


def server_program():
    # get the hostname
    host = socket.gethostname()
    port = 5000  # initiate port no above 1024

    server_socket = socket.socket()  # get instance
    # look closely. The bind() function takes tuple as argument
    server_socket.bind((host, port))  # bind host address and port together

    # configure how many client the server can listen simultaneously
    server_socket.listen(2)
    conn, address = server_socket.accept()  # accept new connection
    print("Connection from: " + str(address))
    while True:
        # receive data stream. it won't accept data packet greater than 1024 bytes
        data = conn.recv(1024).decode()
        if not data:
            # if data is not received break
            break
        print("from connected user: " + str(data))
        data = input(' -> ')
        conn.send(data.encode())  # send data to the client

    conn.close()  # close the connection


if __name__ == '__main__':
    server_program()

So our python socket server is running on port 5000 and it will wait for client request. If you want the server to not quit when the client connection is closed, just remove the if condition and break the statement. Python while loop is used to run the server program indefinitely and keep waiting for client request.

Python Socket Client

We will save python socket client program as socket_client.py. This program is similar to the server program, except binding. The main difference between server and client program is, in server program, it needs to bind host address and port address together. See the below python socket client example code, the comment will help you to understand the code.

import socket


def client_program():
    host = socket.gethostname()  # as both code is running on same pc
    port = 5000  # socket server port number

    client_socket = socket.socket()  # instantiate
    client_socket.connect((host, port))  # connect to the server

    message = input(" -> ")  # take input

    while message.lower().strip() != 'bye':
        client_socket.send(message.encode())  # send message
        data = client_socket.recv(1024).decode()  # receive response

        print('Received from server: ' + data)  # show in terminal

        message = input(" -> ")  # again take input

    client_socket.close()  # close the connection


if __name__ == '__main__':
    client_program()

Python Socket Programming Output

To see the output, first run the socket server program. Then run the socket client program. After that, write something from client program. Then again write reply from server program. At last, write bye from client program to terminate both program. Below short video will show how it worked on my test run of socket server and client example programs. python socket programming, python socket server

python3.6 socket_server.py 
Connection from: ('127.0.0.1', 57822)
from connected user: Hi
 -> Hello
from connected user: How are you?
 -> Good
from connected user: Awesome!
 -> Ok then, bye!
python3.6 socket_client.py 
 -> Hi
Received from server: Hello
 -> How are you?
Received from server: Good
 -> Awesome!
Received from server: Ok then, bye!
 -> Bye

Notice that socket server is running on port 5000 but client also requires a socket port to connect to the server. This port is assigned randomly by client connect call. In this case, it’s 57822.

Handling Multiple Clients Using Threads

A server can utilize threads to handle multiple clients simultaneously. This approach allows the server to process multiple client connections concurrently, enhancing its responsiveness and ability to handle a large number of clients efficiently.

Here’s an example of how this can be achieved in Python using the threading module:

import threading

def handle_client(client_socket):
    # This function is responsible for handling each client connection.
    # It sends a greeting message to the client and then closes the connection.
    client_socket.send(b"Hello, Client!")
    client_socket.close()

while True:
    # The server continuously listens for incoming client connections.
    client_socket, addr = server_socket.accept()
    # When a new client connects, a new thread is created to handle the client.
    client_thread = threading.Thread(target=handle_client, args=(client_socket,))
    client_thread.start()

Blocking vs. Non-Blocking Sockets

In socket programming, sockets can operate in either blocking or non-blocking mode. This mode determines how the socket behaves when it is waiting for data to be received or sent.

  • Blocking Sockets: In blocking mode, operations like recv() and accept() will block the execution of the program until data is available. This means that the program will pause and wait for data to be received or a connection to be accepted. While this approach is simple to implement, it can lead to performance issues and unresponsiveness in applications that need to handle multiple tasks concurrently.

  • Non-Blocking Sockets: Non-blocking sockets, on the other hand, return immediately if no data is available. This approach prevents the application from freezing or becoming unresponsive due to socket operations. Non-blocking sockets are particularly useful in applications that need to handle multiple connections simultaneously, such as web servers or chat servers. However, they require more complex programming to handle the asynchronous nature of the operations.

TCP vs. UDP Sockets

Feature TCP (Transmission Control Protocol) UDP (User Datagram Protocol)
Connection Connection-oriented Connectionless
Reliability Reliable, ensures data integrity Unreliable, no guarantee
Use Cases Web browsing, file transfer Video streaming, gaming

TCP (Transmission Control Protocol) and UDP (User Datagram Protocol) are two fundamental protocols in the internet protocol suite that enable communication over the internet. The key differences between them lie in their approach to establishing connections and ensuring data reliability.

TCP is a connection-oriented protocol, which means a connection is established between the sender and receiver before data is sent. This ensures that data packets are delivered in the correct order and retransmitted if lost or corrupted, ensuring data integrity. TCP is suitable for applications that require guaranteed delivery of data, such as web browsing and file transfer.

On the other hand, UDP is a connectionless protocol, which means there is no dedicated end-to-end connection. Data is sent as a series of packets, and the receiver does not send an acknowledgement. This makes UDP faster but less reliable than TCP. UDP is commonly used for applications that require fast transmission and can tolerate some loss of data, such as video streaming and gaming.

Common Errors and Debugging

  1. Address Already in Use Error: This error occurs when a process is already using the port you’re trying to use. To resolve this, you can use the following command to identify the process using the port:

    netstat -an | grep <port_no>
    

    Once you’ve identified the process, you can either stop the process or use a different port for your application.

  2. Connection Refused Error: This error occurs when the client attempts to connect to a server that is not running or not listening on the specified port. To resolve this, ensure that the server is running and listening on the correct port before the client attempts to connect. Here’s a simple Python code block to check if a server is listening on a specific port:

import socket

def check_port(host, port):
    try:
        with socket.create_connection((host, port), timeout=1):
            print(f"Port {port} is open on {host}")
    except socket.timeout:
        print(f"Port {port} is not open on {host}")

# Example usage
check_port('localhost', 5000)

3. Exception Handling: It's essential to handle exceptions properly to ensure your application can recover from errors gracefully. Here's an example of how to handle a connection error using a `try-except` block:

```python
try:
    client_socket.connect(('localhost', 12345))
except socket.error as e:
    print(f"Error connecting to the server: {e}")
    # Optionally, you can add additional error handling or logging here
except Exception as e:
    print(f"An unexpected error occurred: {e}")
    # Optionally, you can add additional error handling or logging here

FAQs

1. How to create a Python socket server?

Here is an example of how to create a Python socket server:

import socket

def server_program():
    # get the hostname
    host = socket.gethostname()
    port = 5000  # initiate port no above 1024

    server_socket = socket.socket()  # get instance
    # look closely. The bind() function takes tuple as argument
    server_socket.bind((host, port))  # bind host address and port together

    # configure how many clients the server can listen to simultaneously
    server_socket.listen(2)
    conn, address = server_socket.accept()  # accept new connection
    print("Connection from: " + str(address))
    while True:
        # receive data stream. it won't accept data packets greater than 1024 bytes
        data = conn.recv(1024).decode()
        if not data:
            # if data is not received break
            break
        print("from connected user: " + str(data))
        data = input(' -> ')
        conn.send(data.encode())  # send data to the client

    conn.close()  # close the connection


if __name__ == '__main__':
    server_program()

2. How to create a Python socket client?

import socket

def client_program():
    host = socket.gethostname()  # as both code is running on same pc
    port = 5000  # socket server port number

    client_socket = socket.socket()  # instantiate
    try:
        client_socket.connect((host, port))  # connect to the server
    except ConnectionRefusedError:
        print("The connection to the server was refused")
        return

    message = input(" -> ")  # take in the user's message

    while message.lower().strip() != 'bye':
        client_socket.send(message.encode())  # send in the socket and in networking
        data = client_socket.recv(1024).decode()  # receive in the client
        print('Response from the server : ' + data)  # in the client console
        message = input(" -> ")

    client_socket.close()  # close the connection

if __name__ == '__main__':
    client_program()

3. What is the difference between TCP and UDP sockets?

TCP provides reliable communication, whereas UDP is faster but does not guarantee data integrity.

Feature TCP UDP
Connection Connection-oriented Connectionless
Reliability Guaranteed delivery No guarantee of delivery
Ordering Ensures correct order No guarantee of order
Error-checking Error-checking and correction Error-checking but no correction
Speed Slower due to error-checking Faster due to no error-checking
Use Cases File transfer, email, web browsing Online gaming, video streaming, VoIP

4. How to handle multiple clients in Python socket programming?

You can use threading to manage multiple clients concurrently. Here’s an example of how you can modify the server code to handle multiple clients using threading:

import threading

def handle_client(conn, addr):
    print(f"New connection from {addr}")
    while True:
        try:
            message = conn.recv(1024).decode()
            if message:
                print(f"Message from {addr}: {message}")
                conn.send(message.encode())
        except Exception as e:
            print(f"Error with connection from {addr}: {e}")
            break
    conn.close()

def server_program():
    host = socket.gethostname()
    port = 5000

    server_socket = socket.socket()
    server_socket.bind((host, port))
    server_socket.listen()

    while True:
        conn, addr = server_socket.accept()
        thread = threading.Thread(target=handle_client, args=(conn, addr))
        thread.start()
        print(f"Active connections: {threading.activeCount() - 1}")

if __name__ == '__main__':
    server_program()

Conclusion

In this tutorial, you learned the basics of Python socket programming, including how to create a simple client-server architecture, handle multiple clients using threading, and understand the differences between TCP and UDP sockets. To further enhance your knowledge of Python programming, we recommend checking out the following tutorials:

  1. Python Tutorial
  2. What is a Socket?
  3. Understanding Sockets

These tutorials will provide you with a deeper understanding of Python and its applications in network programming.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors
Default avatar
Pankaj

author


Default avatar

Sr Technical Writer

Senior Technical Writer @ DigitalOcean | 2x Medium Top Writers | 2 Million+ monthly views & 34K Subscribers | Ex Cloud Consultant @ AMEX | Ex SRE(DevOps) @ NUTANIX


Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
JournalDev
DigitalOcean Employee
DigitalOcean Employee badge
April 28, 2018

Great little example, thanks!

- Synapsensalat

    JournalDev
    DigitalOcean Employee
    DigitalOcean Employee badge
    May 28, 2018

    Good Job

    - amrane

      JournalDev
      DigitalOcean Employee
      DigitalOcean Employee badge
      June 25, 2018

      your server code is wrong you have to use different names to send the data data = conn.recv(1024).decode() if not data: # if data is not received break break print("from connected user: " + str(data)) message = input(’ -> ') #use different name at the place of data conn.send(message.encode()) # send data to the client

      - Aditya raj

        JournalDev
        DigitalOcean Employee
        DigitalOcean Employee badge
        November 13, 2018

        host = socket.gethostname() AttributeError: ‘module’ object has no attribute ‘gethostname’ im getting this error…

        - adarsh

          JournalDev
          DigitalOcean Employee
          DigitalOcean Employee badge
          January 21, 2019

          how do we chat with different persons should we give client program to them and ask to run. and i would host the server in my pc

          - sanjay

            JournalDev
            DigitalOcean Employee
            DigitalOcean Employee badge
            April 21, 2019

            File “C:/Users/hp/AppData/Local/Programs/Python/Python37/socket_client.py”, line 25, in client_program() File “C:/Users/hp/AppData/Local/Programs/Python/Python37/socket_client.py”, line 9, in client_program client_socket.connect((host, port)) # connect to the server ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it I am getting this error

            - sdd

              JournalDev
              DigitalOcean Employee
              DigitalOcean Employee badge
              May 5, 2019

              It works at localhost nicely… Thank you friend

              - Basavaraj Hunshal

                JournalDev
                DigitalOcean Employee
                DigitalOcean Employee badge
                May 31, 2019

                Simple example. Thanks.

                - Nagesh

                  JournalDev
                  DigitalOcean Employee
                  DigitalOcean Employee badge
                  November 30, 2019

                  Very nice from you. Just carry on.

                  - Mihailo Raskovic

                    JournalDev
                    DigitalOcean Employee
                    DigitalOcean Employee badge
                    April 14, 2020

                    How can we compile this with python not with python3

                    - bindu

                      Try DigitalOcean for free

                      Click below to sign up and get $200 of credit to try our products over 60 days!

                      Sign up

                      Join the Tech Talk
                      Success! Thank you! Please check your email for further details.

                      Please complete your information!

                      Become a contributor for community

                      Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

                      DigitalOcean Documentation

                      Full documentation for every DigitalOcean product.

                      Resources for startups and SMBs

                      The Wave has everything you need to know about building a business, from raising funding to marketing your product.

                      Get our newsletter

                      Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

                      New accounts only. By submitting your email you agree to our Privacy Policy

                      The developer cloud

                      Scale up as you grow — whether you're running one virtual machine or ten thousand.

                      Get started for free

                      Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

                      *This promotional offer applies to new accounts only.