2

我正在使用以下脚本:

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 改变了你使用数据包的方式。我觉得有点绝望。帮助?谢谢

4

1 回答 1

2

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

packet = b""

代替packet = ""

于 2012-09-03T11:59:12.840 回答