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)