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