Commits

Yuya Nishihara  committed 0c040ac

python: add little server to dump received data

  • Participants
  • Parent commits cdeb9e2

Comments (0)

Files changed (1)

File python/dumpserver.py

+#!/usr/bin/env python
+"""A little TCP server to dump received packet"""
+import optparse, socket, sys, traceback
+
+def hexdump(s):
+    return ' '.join('%02x' % ord(c) for c in s)
+
+def handleconn(conn, address):
+    while True:
+        s = conn.recv(8192)
+        if not s:
+            return
+        print '%s: %s %r' % (address[0], hexdump(s), s)
+
+def run(port):
+    sock = socket.socket()
+    sock.bind(('0.0.0.0', port))
+    sock.listen(1)
+    while True:
+        try:
+            handleconn(*sock.accept())
+        except KeyboardInterrupt:
+            raise
+        except Exception, err:
+            print >>sys.stderr, '-' * 72
+            traceback.print_exc()
+            print >>sys.stderr, '-' * 72
+
+def main():
+    op = optparse.OptionParser()
+    op.add_option('-p', '--port', type='int', default=9101)
+    opts, args = op.parse_args()
+    run(opts.port)
+
+if __name__ == '__main__':
+    main()