私はボールを転がします:
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, "<this will be 9 spaces away from the '5'>"
..それで遊んでください。
ご不明な点がございましたら、お知らせください。私はあなたがやっていることが好きです、私の古いプロジェクトを思い出させます. 私があなたのようなきちんとしたプロトコル仕様を持っていなかったことを除けば、ラッキー野郎。;)