Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python-发送数据包发出错误-Minecraft数据包_Python_Struct_Send_Packet_Minecraft - Fatal编程技术网

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
。这不是问题。