【问题标题】:Python - Sending Packets issuing error - Minecraft PacketsPython - 发送数据包发出错误 - Minecraft Packets
【发布时间】:2012-08-28 03:46:27
【问题描述】:

我正在使用以下脚本:

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 struct send packet minecraft


    【解决方案1】:

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

    packet = b""
    

    而不是packet = ""

    【讨论】:

    • 所以,不是 packet += struct.pack("B", 1),而是 packet = b"1"
    • @EvanJerald 不,b'\x01'。在 Python 3 中随意使用 struct。这不是问题。
    猜你喜欢
    • 1970-01-01
    • 2012-10-05
    • 2020-09-03
    • 1970-01-01
    • 1970-01-01
    • 2012-09-02
    • 2012-06-07
    • 1970-01-01
    • 2015-08-25
    相关资源
    最近更新 更多