0
我正在尝试使用Python编写套接字程序。在我的客户端我有这个节:Python套接字程序
clientSocket = socket(AF_INET, SOCK_STREAM)
clientSocket.connect((serverName, serverPort))
linesInBytes = clientSocket.recv(1024)
print(linesInBytes)
,在我的服务器端我有:
connectionSocket, addr = serverSocket.accept()
#Set secret word
word = 'Arkansas'
linesForString = ''
#Prints out number of letters
for x in word:
linesForString += '_ '
linesInBytes = linesForString.encode('utf-8')
connectionSocket.send(linesInBytes)
以及当它打印出在客户端的一些原因,它打印出:
b'_ _ _ _ _ _ _'
而且我没有在代码中打印出b的地方。这个b来自哪里? 谢谢!
尝试打印linesInBytes之前发送它,看看它包含什么,请阅读https://stackoverflow.com/questions/6269765/what-does-the-b-character-do-in-front-of-a-string -literal – JLev
另外,你真的需要编码吗? – JLev
在Python'b'....''中表示一个字节字符串(即,一串没有关联编码的原始字节)。见例如[这个问题](https://stackoverflow.com/questions/6224052/what-is-the-difference-between-a-string-and-a-byte-string)。 – larsks