2013-07-06 138 views
0

我想通過套接字寫一個字節(0-255),但我無法弄清楚如何。Python通過套接字寫字節

socket.send(str(unichr(byte)))適用於0-128,然後給出UnicodeEncodeError

無論如何要寫一個字節在套接字上嗎?提前致謝。

+1

難道你不能只寫入字節的套接字,並將其編碼字符串在接收端? – Nadh

回答

1

使用正常的字節串。

socket.send('\xa5') 
socket.send('Hello, world!') 

chr()

socket.send(chr(0xa5)) 
0

它實際上不是被賦予錯誤這是STR功能的插座:

>>> str(unichr(200)) 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
UnicodeEncodeError: 'ascii' codec can't encode character u'\xc8' in position 0: ordinal not in range(128) 
>>> unicode(unichr(200)) 
u'\xc8' 

嘗試發送該網址。

+0

字節應通過套接字發送,而不是文本。 –