0

I have a Python server running that listen to data that is sent by the Python client. The client takes input from the user and sends it to the server which prints it. However, I get the error that says "TyperError: a byte-like object is required, not 'str'". It is on line number 8 of the client code.

SERVER CODE:

import socket

def server(interface, port):
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    sock.bind((interface, port))
    sock.listen(1)
    print('Listening at', sock.getsockname())
    while True:
        sc, sockname = sock.accept()
        print('We have accepted a connection from', sockname)
        print(' Socket name:', sc.getsockname())
        print(' Socket peer:', sc.getpeername())
        message = sc.recv(1024)
        print(' Incoming sixteen-octet message:', repr(message))
        sc.sendall(b'Farewell, client')
        sc.close()
        print(' Reply sent, socket closed')

if __name__ == '__main__':
    server('0.0.0.0', 9999)

CLIENT CODE:

import socket

def client(host, port):
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.connect((host, port))
    print('Client has been assigned socket name', sock.getsockname())
    command = input("message > ")
    sock.sendall(command)
    reply = sock.recv(1024)
    print('The server said', repr(reply))
    sock.close()

if __name__ == '__main__':
    client('localhost', 9999)

2 Answers 2

1

TyperError: a byte-like object is required, not 'str'

socket.sendall expects bytes, but you have passed in the result of input("message > "), which is a str. So, you need to somehow convert the str from the input into bytes. You can use encode to do this:

command = input("message > ").encode()

encode, well, encodes a str into bytes.

Sign up to request clarification or add additional context in comments.

Comments

0

While communicating in Python, you should encode the parameter you send as a byte and decode the parameter you received from the byte format. You can understand what I want to say from the code below.

Server Code:

import socket

def server(interface, port):
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    sock.bind((interface, port))
    sock.listen(1)
    print('Listening at', sock.getsockname())
    while True:
        sc, sockname = sock.accept()
        print('We have accepted a connection from', sockname)
        print(' Socket name:', sc.getsockname())
        print(' Socket peer:', sc.getpeername())
        message = sc.recv(1024).decode('utf-8')
        print(' Incoming sixteen-octet message:', repr(message))
        sc.sendall(bytes('Farewell, client','utf-8'))
        sc.close()
        print(' Reply sent, socket closed')

if __name__ == '__main__':
    server('0.0.0.0', 9999)

Client Code:

import socket

def client(host, port):
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.connect((host, port))
    print('Client has been assigned socket name', sock.getsockname())
    command = input("message > ")
    sock.sendall(bytes(command,'utf-8'))
    reply = sock.recv(1024).decode('utf-8)
    print('The server said', repr(reply))
    sock.close()

if __name__ == '__main__':
    client('localhost', 9999)

If you edit your code this way, I think you will correct the error you received. I hope it helps as I am new to this. Sorry for my entry-level English.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.