Ad

Get A Response Fro Server

- 1 answer

I'm trying to make a server-client program, where server will listen to client's messages and depends on the message, will response. I send a message from client with username and content, server accept it and print a message to sending to client Till here is everything fine. But when it comes to sending a message server will throw and error:

 `TypeError: byte indices must be integers or slices, not str`

It looks like this line is the problem, but I'm not sure....

`clientsocket.send(msg['header'] + msg['data'])`

here is a whole server code. Please let me know, if client code is necessary too please.

import socket
import time
import pickle
import select

HEADERSIZE = 10
IP = "127.0.0.1"
PORT = 1234


s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((IP, PORT))
s.listen()
sockets_list = [s]
clients = {}

# Handles message receiving
def receive_message(clientsocket):
    try:
        message_header = clientsocket.recv(HEADERSIZE)
        if not len(message_header):
            return False

        message_length = int(message_header.decode('utf-8').strip())
        return {'header': message_header, 'data': clientsocket.recv(message_length)}

    except:
        return False


while True:
    read_sockets, _, exception_socket = select.select(sockets_list, [], sockets_list)
    for notified_socket in read_sockets:
        if notified_socket == s:
            clientsocket, address = s.accept()
            user = receive_message(clientsocket)
            if user is False:
                continue

            sockets_list.append(clientsocket)
            clients[clientsocket] = user

            print(f"Connection from {address[0]}:{address[1]} has been estabilished! User:{user['data'].decode('utf-8')}")


        else:
            message = receive_message(notified_socket)

            if message is False:
                print(f"Close connection from {clients[notified_socket]['data'].decode('utf-8')}")
                sockets_list.remove(notified_socket)
                del clients[notified_socket]
                continue

            user = clients[notified_socket]
            #message_decoded = message['data'].decode('utf-8')
            print(f'Received message from {user["data"].decode("utf-8")}: {message["data"].decode("utf-8")}')

            for clientsocket in clients:
                if clientsocket == notified_socket:
                    if message["data"].decode("utf-8") == "y":
                        #d = {1: "Hey", 2: "there"}

                        msg = pickle.dumps("th.jpeg")
                        print(msg)

                        msg = bytes(f'{len(msg):<{HEADERSIZE}}', "utf-8") + msg
                        clientsocket.send(msg['header'] + msg['data'])

                    else:
                        d = {1: "Hey", 2: "there"}
                        msg = pickle.dumps(d)
                        print(msg)
                        # msg = bytes(f'{len(msg):<{HEADERSIZE}}', "utf-8") + msg
                        clientsocket.send(msg['header'] + msg['data'])

    for notified_socket in exception_socket:
        sockets_list.remove(notified_socket)
        del clients[notified_socket]

HERE is a whole error code:

Connection from 127.0.0.1:48480 has been estabilished! User:j
Received message from j: y
b'\x80\x03X\x07\x00\x00\x00th.jpegq\x00.'
Traceback (most recent call last):
  File "server.py", line 69, in <module>
    clientsocket.send(msg['header'] + msg['data'])
TypeError: byte indices must be integers or slices, not str

As You can see, it works till sending the message line

Ad

Answer

msg = pickle.dumps("th.jpeg") will encode the string "th.jpeg" as a bytes-object. msg = bytes(f'{len(msg):<{HEADERSIZE}}', "utf-8") + msg just adds that bytes object to another bytes-object.

So msg is a simple bytes-object, not any kind of server packet or similar. Therefor it is not possible to subscribe msg with msg['header'] or any other string.

Your code seems a little weird but maybe just try this line:

clientsocket.send(msg)

Since you are already converting msg to a bytes-object, it can be sent to the client directly. You just have to decode it properly in the client.

Ad
source: stackoverflow.com
Ad