In Python 3.5, using sockets, I have:
message = 'HTTP/1.1 200 OK\nContent-Type: text/html\n\n'
s.send(message.encode())
How can I do that in one line? I ask because I had:
s.send('HTTP/1.1 200 OK\nContent-Type: text/html\n\n')
but in Python 3.5 bytes are required, not a string, so this gives the error:
builtins.TypeError: a bytes-like object is required, not 'str'
Should I not be using send?
str.encode()call too, you know:s.send('HTTP/1.1 200 OK\nContent-Type: text/html\n\n'.encode('ascii')). Not that you need to, what withb'...'byte literals..encode()on the return value ofs.send(). Look closely at the parentheses;message.encode()is entirely contained insides.send(). So is my version withs.send('....'.encode('utf8')).