How to get response in a Telnet-Client to python socket connection?

I have to send and recieve data from a limited hardware setup running linux. I thought to use Telnet utility since it was available in the device (No netcat or anything similar available).
The Bash script which I used to send and recieve data looks like this.

#!/bin/bash

# IP address and port of the server
TELNET_SERVER="192.168.0.111"
TELNET_PORT="12345"

while true; do
    # Running command to obtain required data
    data=$(./wrs_dump_shmem -M)
    message="$data"

    # Send message via Telnet to the server and receive response

    response=$(echo "$message" | telnet "$TELNET_SERVER" "$TELNET_PORT" 2>&1 | tail -n 1)

    # Extract adjust and offset values from response
    adjust=$(echo "$response" | awk -F ', ' '{print $1}' | cut -d ':' -f 2)
    offset=$(echo "$response" | awk -F ', ' '{print $2}' | cut -d ':' -f 2)

    # Check if adjust is True
    if [ "$adjust" == "True" ]; then
        # Run ppsi_conf command with offset value
        ./ppsi_conf 4 "$offset"
        echo "PPSI configuration adjusted with offset: $offset"
    elif [ "$adjust" == "False" ]; then
        echo "No adjustment needed. Continuing..."
    else
        echo "Invalid response: $response"
        exit 1
    fi

    #Sleeping for 0.25 Seconds. 
    usleep 250000  
done

I made a Python server to send and recieve data in my machine

import socket
from datetime import datetime

ip0 = "192.168.0.120"
ip1 = "192.168.0.121"
ip2 = "192.168.0.122"
M12 = 0
M21 = 0
M13 = 0
M31 = 0


def save_to_file(ip, message):
    filename = f"{ip}.txt"
    with open(filename, "a") as file:
        timestamp = datetime.now().strftime("[%Y-%m-%d %H:%M:%S]")
        file.write(f"{timestamp} {message}n")

def send_response(client_socket, adjust, offset):
    response = f"Adjust:{adjust},offset:{offset}r"
    client_socket.send(response.encode())
    print(f"Response sent: {response}")

def extract_final_numbers(data):
    # Split the data by newlines
    lines = data.strip().split('n')
    final_numbers = []
    # Iterate over each line
    for line in lines:
        # Split the line by colon
        parts = line.split()
        # Extract the final numbers (excluding the timestamp)
        numbers = [part.split(':')[1] for part in parts if ':' in part]
        final_numbers.extend(numbers)
    return final_numbers

def listen(port):
    offset = 0
    # Create a socket object
    server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
   
    # Bind the socket to a host and port
    server_socket.bind(('0.0.0.0', port))

    # Listen for incoming connections
    server_socket.listen(5)
    print(f"Listening on port {port}...")

    while True:
        # Accept incoming connection
        client_socket, client_address = server_socket.accept()
        print(f"Connection from {client_address}")

        # Receive data from client
        data = client_socket.recv(1024).decode("utf-8").strip()

        
        if data:
           # Save data to file
            save_to_file(client_address[0], data)           
            f=extract_final_numbers(data)
            if(client_address[0]==ip0):
                M12 = int(f[0])
            if(client_address[0]==ip2):
                M13 = int(f[0])
            if(client_address[0]==ip1):
                M31 = int(f[1])
                M21 = int(f[2])
                offset =int( (M21-M12+M31-M13)/6 )
            # Check if adjust should be True or False based on the client IP
            adjust = "True" if client_address[0] == ip1 else "False"

            # Send response
            send_response(client_socket, adjust, offset)

        # Close the connection
        client_socket.close()

if __name__ == "__main__":
    PORT = 12345  # port number
    listen(PORT)

I am able to recieve the data from the client as expected but unable to give a response.
The Bash script doesn’t seems to recieve any response from the server. Can anyone help me point out the problem.
I am not familiar with the Telent or socket programming. The documentations I coudl find in the internet hasn’t been helpful so far. Thank you in advance.

I thought of using telnetlib in python. But I could only find method to establish a client side application. I tried changing the formatting, assuming telnet is not parsing the response properly. I used wireshark to sniff the packets that is being exchanged and I could see that my machine is sending the packets to the remote client.

Wireshark Output:

Frame 975: 87 bytes on wire (696 bits), 87 bytes captured (696 bits) on interface wlp0s20f3, id 0
Ethernet II, Src: IntelCor_ec:0e:be (00:d4:9e:ec:0e:be), Dst: SevenSol_08:67 (64:fb:81:20:08:67)
Internet Protocol Version 4, Src: 192.168.0.111, Dst: 192.168.0.120
Transmission Control Protocol, Src Port: 12345, Dst Port: 49152, Seq: 1, Ack: 10, Len: 21
    Source Port: 12345
    Destination Port: 49152
    [Stream index: 7]
    [Conversation completeness: Complete, WITH_DATA (63)]
    [TCP Segment Len: 21]
    Sequence Number: 1    (relative sequence number)
    Sequence Number (raw): 4022264318
    [Next Sequence Number: 22    (relative sequence number)]
    Acknowledgment Number: 10    (relative ack number)
    Acknowledgment number (raw): 3254986498
    1000 .... = Header Length: 32 bytes (8)
    Flags: 0x018 (PSH, ACK)
    Window: 128
    [Calculated window size: 65536]
    [Window size scaling factor: 512]
    Checksum: 0x8273 [unverified]
    [Checksum Status: Unverified]
    Urgent Pointer: 0
    Options: (12 bytes), No-Operation (NOP), No-Operation (NOP), Timestamps
    [Timestamps]
    [SEQ/ACK analysis]
    TCP payload (21 bytes)
Data (21 bytes)
    Data: 41646a7573743a46616c73652c6f66667365743a30
    [Length: 21]

I hope someone can point out my mistake.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật