Python-发送数据包发出错误-Minecraft数据包
我正在使用以下脚本:Python-发送数据包发出错误-Minecraft数据包,python,struct,send,packet,minecraft,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 pack
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
并得到以下回复:
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语言。我仔细阅读并发现了一些关于Python3改变数据包使用方式的信息。我觉得有点绝望。帮忙?谢谢在python 3中,您必须隐式地将字符串
数据包定义为字节
packet = b""
因此,不是packet+=struct.pack(“B”,1),而是packet=B“1”@EvanJerald Nope,B'\x01'
。在Python3中随意使用struct
。这不是问题。