RSA Encrypting through socket programming

Basically, I’ve got a task to send a message from client to server via assymetric encryption. I’ve studied some things about RSA and tried to implement it through creating public keys and private keys for server and client. However I’ve got problems with encoding and encrypting, and I always get errors.

import socket #file for client
import rsa
#192.168.1.1

PORT = 2020
HEADER = 2048
DISCONNECT_MESSAGE="!DISCONNECT"
SERVER = socket.gethostbyname(socket.gethostname())
client = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
ADDR = (SERVER,PORT)
client.connect(ADDR)

public_key, private_key = rsa.newkeys(1024)


with open("public_client.pem", "wb") as f:
    f.write(public_key.save_pkcs1("PEM"))

with open("private_client.pem", "wb") as f:
    f.write(private_key.save_pkcs1("PEM"))

with open("public_server.pem", "rb") as f:
    public_key_server = rsa.PublicKey.load_pkcs1(f.read())

def send(msg): 
    encrypted_message = rsa.encrypt(msg.encode('utf-8'), public_key_server) #here tried to implement rsa
    msg_length = len(encrypted_message)
    send_length = str(msg_length).encode('utf-8')
    send_length += b' ' * (HEADER - len(send_length))
    client.send(send_length)
    client.send(encrypted_message)
    print(client.recv(2048).decode('utf-8'))

send("place for message")
send(DISCONNECT_MESSAGE)
import socket
import threading #for server
import rsa
PORT = 2020
HEADER = 2048
SERVER = socket.gethostbyname(socket.gethostname())
ADDR = (SERVER,PORT)
DISCONNECT_MESSAGE="!DISCONNECT"
sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
sock.bind(ADDR)

public_key, private_key = rsa.newkeys(1024)

with open("public_server.pem", "wb") as f:
    f.write(public_key.save_pkcs1("PEM"))

with open("private_server.pem", "wb") as f:
    f.write(private_key.save_pkcs1("PEM"))


def handle_client(conn, addr):
    print("[NEW CONNECTION] {addr} connected.")

    connected = True
    while connected:
        msg_length = conn.recv(2048).decode('utf-8')
        if msg_length:
            msg_length = int(msg_length)
            msg = conn.recv(msg_length).decode('utf-8')
            encrypted_message = msg
            clear_message = rsa.decrypt(msg, private_key)
            if clear_message == DISCONNECT_MESSAGE:
                connected = False
            print(f"[{addr}] {msg}")
            conn.send("Msg received".encode())
    conn.close()



def start():

    sock.listen()
    print(f"[LISTENING] Server listening on {SERVER}")
    while True:
        conn, addr = sock.accept()
        #when a new connection occurs we pass the connection to handle client
        thread = threading.Thread(target = handle_client, args = (conn,addr))
        thread.start()
        print(f"[ACTIVE CONNECTIONS] {threading.active_count() - 1}")

print("[STARTING] server is starting...")
start()

So basically, the question is: how can I implement RSA in Socket programming?