我会让球滚动:
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)
如果您从未使用过格式字符串,那么“%-20s”可能对您来说很奇怪.实质上..
print "%s" % 5
..将打印出5 ..
print "%10s" % 5
..会将输出填充为正好10个字符的宽度.但它在右侧填充它们,我们希望在左侧填充-因此-..
print "%-10s" % s, ""
..玩它.
如果有任何不清楚的地方,请通知我.我喜欢你在做什么,让我想起了我的一个老项目.除了我没有像您一样整洁的协议规范,幸运的混蛋. ?