A str
is an abstract sequence of Unicode code points; a bytes
is a sequence of 8-bit numbers. Python 3 made the distinction between the two very clear and does not allow you to combine them implicitly. A str
may have several valid encodings, and a bytes
object may or may not be the encoding of a valid Unicode string. (Or, the bytes
could be the encoding of multiple different str
objects depending on the encoding used to create it.)
'GET '
and user_url
are str
objects, while ' HTTP/1.0
'.encode()
is a bytes
object. You want to encode the entire concatenated string instead.
cmd = 'GET {} HTTP/1.0
'.format(user_url).encode()
Or perhaps written to show the steps more clearly,
cmd = 'GET {} HTTP/1.0
'.format(user_url) # still a str
mysock.send(cmd.encode()) # send the encoding of the str
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…