chat server raises TypeError: descriptor 'encode' requires a 'str' object but received a 'unicode'

13,735

This is not a client/server problem.

The actual error I received on testing your script was:

File "chat.py", line 42, in clientthread conn.sendall(str.encode(reply)) TypeError: descriptor 'encode' requires a 'str' object but received a 'unicode'

Generally, it would be useful to post the full error message when problems occur....

A bit of google searching on the error and following the discussion at Python - Descriptor 'split' requires a 'str' object but received a 'unicode'

I changed

conn.sendall(str.encode(reply))

to

conn.sendall(reply.encode('ascii'))

and now it works fine for me using telnet localhost 8888 as the client.

Share:
13,735
OmniBean
Author by

OmniBean

Updated on June 10, 2022

Comments

  • OmniBean
    OmniBean almost 2 years

    I am pretty much a beginner to Python Socket programming. I made a chat server, but it doesn't work right.

    It works fine with receiving data, but does not work with sending data. When i use the 'conn.send()', the client never receives the message. Please help me.

    This is my code for the socket server:
    
    '''
        Simple socket server using threads
    '''
    
    import socket
    import sys
    from _thread import *
    
    HOST = ''   # Symbolic name meaning all available interfaces
    PORT = 8888 # Arbitrary non-privileged port
    
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    print ( 'Socket created on Port: '+str(PORT))
    
    #Bind socket to local host and port
    try:
        s.bind((HOST, PORT))
    except socket.error as msg:
        print ( 'Bind failed. Error Code : ' + str(msg[0]) + ' Message ' + msg[1])
        sys.exit()
    
    print ( 'Socket bind complete')
    
    #Start listening on socket
    s.listen(10)
    print ( 'Socket now listening')
    
    connectmsg = 'Welcome to OmniBean\'s Chat server!'
    #Function for handling connections. This will be used to create threads
    def clientthread(conn):
        #Sending message to connected client
        print('Sending Welcome Message...')
        #print(conn)
        conn.send(str.encode(connectmsg)) #send only takes string ENCODED!
    
        #infinite loop so that function do not terminate and thread do not end.
        while True:
    
            #Receiving from client
            data = bytes.decode(conn.recv(1024))
            print (data)
            reply = 'OK...' + data
            if not data:
                break
    
            conn.sendall(str.encode(reply))
    
        #came out of loop
        conn.close()
    
    #now keep talking with the client
    while 1:
        #wait to accept a connection - blocking call
        conn, addr = s.accept()
        print ( 'Connected with ' + addr[0] + ':' + str(addr[1]))
        conn.send(str.encode(connectmsg)) 
        #start new thread takes 1st argument as a function name to be run, second is the tuple of arguments to the function.
        start_new_thread(clientthread ,(conn,))
    
    s.close()
    

    If you can figure out why this is, then can you let me know? My client code is here: My client is using the SimpleNet Library from OmniBean

    import os
    from simplenet import *
    myname = input ('Enter a login name: ')
    host = input('Enter Host Name: ')
    port = input('Enter Host Port: ')
    connect(host,port)
    welcome = receive()
    input('Received Message: '+welcome)
    while True:
        os.system('cls')
        #room = receive()
        #print (room)
        msg = input('Enter a message to send to server: ')
        send(myname+': '+msg)
    

    Theoretically, since I send the data twice from the server, the client should receive the data; however, the client just keeps waiting forever for a message from the server that never comes. Please aid me in solving this problem.

  • OmniBean
    OmniBean over 9 years
    Thank you very much! I am forever grateful you saved my chat server. However, on my machine, there was no error!
  • Rohit Nishad
    Rohit Nishad almost 4 years
    It's working properly, but can you please explain in depth.