我正在使用以下脚本:
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/