2017-10-15 4 views
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はどこから来ますか? ありがとうございました!

+0

linesInBytesを送信する前に印刷し、内容を確認してください。https://stackoverflow.com/questions/6269765/what-does-the-b-character-do-in-front-of-a-string -literal – JLev

+0

また、本当にエンコーディングが必要ですか? – JLev

+1

Pythonで 'b '....'はバイト文字列(すなわち、エンコーディングが関連付けられていない生のバイトの文字列)を表します。例えば、 [この質問](https://stackoverflow.com/questions/6224052/what-is-the-difference-between-a-string-and-byte-string)。 – larsks

答えて

0

.decode('utf8')受信したデータバイトを文字列に戻します。 Python 3は、バイト文字列とUnicode文字列を示すためにb''を表示します。

関連する問題