Python - 发送数据包发出错误 - Minecraft Packets

标签 python struct send packet minecraft

我正在使用以下脚本:

import socket
import struct

username = "username_value"
verification_key = "verification_key"

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)  # boilerplate
s.connect(("example.com", 1234))  # adjust accordingly

# now for the packet
# note that the String type is specified as having a length of 64, we'll pad that

packet = ""

packet += struct.pack("B", 1)  # packet type
packet += struct.pack("B", 7)  # protocol version
packet += "%-64s" % username  # magic!
packet += "%-64s" % verification_key
packet += struct.pack("B", 0)  # that unused byte, assuming a NULL byte here

# send what we've crafted
s.send(packet)

并得到以下回复:

    packet += struct.pack("B", 1)  # packet type
TypeError: Can't convert 'bytes' object to str implicitly

我对 Python 几乎是全新的,刚刚开始,但我理解这门语言。我阅读并发现 Python 3 改变了你使用数据包的方式。我感觉有点绝望了。帮助?谢谢

最佳答案

在 python 3 中,您必须将字符串 packet 隐式定义为字节

packet = b""

而不是packet = ""

关于Python - 发送数据包发出错误 - Minecraft Packets,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12247290/

相关文章:

c - 函数头/声明

c - 通过地址访问结构成员

C++ 非阻塞套接字选择发送太慢?

Javascript - Cordova 应用程序中带有 facebook 插件的 Facebook 消息 - 方法 : "send"

java - 如何在同一 fragment Activity 中将数据从 fragment 发送到 fragment ?

c - 如何正确地 malloc C 中的结构

python - Python中的网络掩码前缀和点分十进制格式转换

python - 使用 scipy.signal.resample 对信号进行重采样

字符串连接上的 Python 3.6 与 3.5 TypeError 消息

python - 在 django 中,你能像在 rails 中一样将 django 应用程序加载到 python 解释器中吗?