Created
August 25, 2017 06:51
-
-
Save Philmist/10f353f5201d12f78fc7b7038bfdf5fb to your computer and use it in GitHub Desktop.
tornadoを使ったPeercastのAtom構造解析
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
# vim: set sw=4 sts=4 ts=4 expandtab: | |
from tornado.tcpserver import TCPServer | |
from tornado.iostream import StreamClosedError | |
from tornado import gen | |
import tornado.ioloop | |
import struct | |
address_state = dict() | |
class PCPServer(TCPServer): | |
"""PCPパケットを読みとって単純にその内容を表示するサーバーです。""" | |
@gen.coroutine | |
def handle_stream(self, stream, address): | |
"""パケットを受けとった時に処理する関数です。""" | |
while True: | |
try: | |
command = yield stream.read_bytes(4) | |
command = command.decode("utf-8") | |
print("PCP: %s" % command) | |
length = yield stream.read_bytes(4) | |
length = struct.unpack("<1I", length) | |
if length[0] > 0x7fffffff: # HAS CHILD | |
length = length[0] & 0x7fffffff # 符号反転 | |
print("CHILD: %s" % length) | |
else: | |
length = length[0] | |
print("LEN: %s" % length) | |
data = yield stream.read_bytes(length) | |
print("DATA READ") | |
except StreamClosedError: | |
break | |
if __name__ == "__main__": | |
server = PCPServer() | |
server.bind(7146) | |
server.start(0) | |
tornado.ioloop.IOLoop.current().start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment