I'm trying to connect to a server using socket module in python. However, I get an error message as shown below.
TypeError: a bytes-like object is required, not 'str'
Here's my code:
import socket
HOST = '0.0.0.0'
PORT = 12345
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server_socket.bind((HOST, PORT))
server_socket.listen(5)
print("\n[*] Listening on port " +str(PORT)+ ", waiting for connexions. ")
client_socket, (client_ip, client_port) = server_socket.accept()
print("[*] Client " +client_ip+ " connected \n")
while True:
try:
command = input(client_ip+ ">")
if(len(command.split()) != 0):
client_socket.send(command)
else:
continue
except(EOFError):
print("Invalid input, type 'help' to get a list of implented commands. \n")
continue
if(command == "quit"):
break
data = client_socket.recv(1024)
print(data + "\n")
client_socket.close()
What causes this TypeError? and what should I do to fix this ?. I'm also open to any advice to improve my python socket code writing.