Since I started using version 3 of Python I have had many problems with sending string through sockets. I know that to send a string in a socket, a 'b'
must be placed before the string to convert it to bytes. But what happens when I have to convert an input()
to bytes? How is it done?
I need to send a message written by keyboard to a socket:
import socket
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect(("localhost",7500))
msg = input()
client.send(msg)
However, when I try it, I get the following error:
TypeError: a bytes-like object is required, not 'str'
Can someone tell me how I convert input()
to bytes? I always use version 2.7 and I do not understand why version 3 is so irritating for the handling of sockets. :(