Python - 发送错误的数据包 - Minecraft数据包

时间:2012-09-03 11:48:36

标签: 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改变你使用数据包的方式。我觉得有点无望。救命?谢谢

1 个答案:

答案 0 :(得分:2)

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

packet = b""

而不是packet = ""