import socket
def Main():
host = '127.0.0.1'
port = 5001
server = ('127.0.0.1',5000)
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind((host, port))
message = input("-> ")
while message != 'q':
s.sendto (message, server)
data, addr = s.recvfrom(1024)
print ('Received from server: ' + (data))
message = input("-> ")
s.close()
if __name__ == '__main__':
Main()
When I run this code the line s.sendto (message, server) causes a TypeError: a bytes-like object is required, not 'str'.
How do you fix this kind of problem? I tried searching the internet but can't find a solution.
bto a string, but in this case the string is coming from user input, so prepending isn't possible. Explicitly encoding is better anyway - prependingbwon't work if the string contains non-ascii characters.