Creating client and server sockets using Python

Today, we will explore how to create client and server sockets using Python, along with an example code.

Client and server sockets are a fundamental part of network communication in computer science. A socket is essentially an endpoint that enables two-way communication between two devices over a network.

Server Socket

The server socket is a program that listens for incoming connections from clients. Once a connection is established, the server creates a new socket object to handle the communication with the client. Here is a basic example of a server socket program in Python:

import socket

HOST = '127.0.0.1'  # Standard loopback interface address (localhost)
PORT = 65432        # Port to listen on (non-privileged ports are > 1023)

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
    s.bind((HOST, PORT))
    s.listen()
    conn, addr = s.accept()
    with conn:
        print('Connected by', addr)
        while True:
            data = conn.recv(1024)
            if not data:
                break
            conn.sendall(data)

In the above code, we first import the socket module and define the IP address and port number for the server. Then we create a new socket object ‘s’ using the AF_INET address family and SOCK_STREAM socket type.

We then bind the socket to our defined IP address and port number, and start listening for incoming connections using the s.listen() method. Once a client connects, the s.accept() method returns a new socket object ‘conn’ representing the connection, along with the address of the client.

We then use a while loop to continuously receive data from the client using conn.recv() and send data back to the client using conn.sendall() until there is no more data to receive.

Client Socket

The client socket is a program that initiates a connection to the server socket. Here is a basic example of a client socket program in Python:

import socket

HOST = '127.0.0.1'  # The server's hostname or IP address
PORT = 65432        # The port used by the server

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
    s.connect((HOST, PORT))
    s.sendall(b'Hello, world')
    data = s.recv(1024)

print('Received', repr(data))

In the above code, we again import the socket module and define the IP address and port number for the server. We then create a new socket object ‘s’ using the AF_INET address family and SOCK_STREAM socket type.

We then initiate a connection to the server using s.connect() and send data to the server using s.sendall(). Finally, we receive data from the server using s.recv() and print the received data.

Conclusion

We have explored how to create client and server sockets using Python along with a basic example code. Sockets are a powerful tool for network communication and are used extensively in a wide range of applications. With the above knowledge, you can build complex network applications in Python.

This is all for now. Hope you enjoy that.

By Asahi



アプリ関連ニュース

お問い合わせはこちら

お問い合わせ・ご相談はお電話、またはお問い合わせフォームよりお受け付けいたしております。

tel. 06-6454-8833(平日 10:00~17:00)

お問い合わせフォーム