Implement log files and stdin comments
This commit is contained in:
parent
897d567334
commit
9d11c29091
3
.gitignore
vendored
3
.gitignore
vendored
@ -1,3 +1,6 @@
|
||||
# captures
|
||||
capture_*.txt
|
||||
|
||||
# Byte-compiled / optimized / DLL files
|
||||
__pycache__/
|
||||
*.py[cod]
|
||||
|
19
handler.py
19
handler.py
@ -7,15 +7,16 @@ d_c = Diff(Color.GREEN, Color.RED)
|
||||
d_s = Diff(Color.BLUE, Color.YELLOW)
|
||||
|
||||
|
||||
def handle(tag: str, data: bytes):
|
||||
msg = data.decode().strip()
|
||||
if msg.startswith("0c") or msg.startswith("0a"):
|
||||
def handle(tag: str, data: str):
|
||||
if tag == "stdin":
|
||||
print(f"[stdin] {data}")
|
||||
if data.startswith("0c") or data.startswith("0a"):
|
||||
if tag == "server":
|
||||
print(f"{Color.YELLOW}{msg}{Color.RESET}")
|
||||
else:
|
||||
print(f"{Color.RED}{msg}{Color.RESET}")
|
||||
print(f"{Color.YELLOW}{data}{Color.RESET}")
|
||||
elif tag == "client":
|
||||
print(f"{Color.RED}{data}{Color.RESET}")
|
||||
else:
|
||||
if tag == "server":
|
||||
d_s.consume(msg)
|
||||
else:
|
||||
d_c.consume(msg)
|
||||
d_s.consume(data)
|
||||
elif tag == "client":
|
||||
d_c.consume(data)
|
||||
|
20
main.py
20
main.py
@ -3,16 +3,26 @@ import os
|
||||
import importlib
|
||||
import traceback
|
||||
import signal
|
||||
import binascii
|
||||
import sys
|
||||
import datetime
|
||||
|
||||
import handler
|
||||
|
||||
SERVER_PORT = 22023
|
||||
|
||||
|
||||
async def queue_handler(queue: asyncio.Queue):
|
||||
def stdin_callback(queue: asyncio.Queue):
|
||||
asyncio.ensure_future(queue.put(("stdin", sys.stdin.readline().rstrip("\n"))))
|
||||
|
||||
|
||||
async def queue_handler(queue: asyncio.Queue, log_filename: str = None):
|
||||
last_edit_timestamp = os.stat(handler.__file__).st_mtime_ns
|
||||
while True:
|
||||
tag, data = await queue.get()
|
||||
if log_filename is not None:
|
||||
with open(log_filename, "a") as f:
|
||||
f.write(f"{tag} {data}\n")
|
||||
try:
|
||||
timestamp = os.stat(handler.__file__).st_mtime_ns
|
||||
if timestamp > last_edit_timestamp:
|
||||
@ -30,7 +40,7 @@ async def queue_handler(queue: asyncio.Queue):
|
||||
|
||||
async def read_to_queue(reader: asyncio.StreamReader, tag: str, queue: asyncio.Queue):
|
||||
while True:
|
||||
data = await reader.readline()
|
||||
data = (await reader.readline()).decode().strip()
|
||||
if not data:
|
||||
break
|
||||
await queue.put((tag, data))
|
||||
@ -40,6 +50,8 @@ async def main():
|
||||
|
||||
queue = asyncio.Queue()
|
||||
|
||||
asyncio.get_event_loop().add_reader(sys.stdin, stdin_callback, queue)
|
||||
|
||||
proc_server = await asyncio.create_subprocess_shell(
|
||||
f'tshark -l -f "udp src port {SERVER_PORT}" -T fields -e data',
|
||||
stdout=asyncio.subprocess.PIPE,
|
||||
@ -64,7 +76,9 @@ async def main():
|
||||
signal.SIGINT, lambda: print("terminating...")
|
||||
)
|
||||
|
||||
handler_task = asyncio.create_task(queue_handler(queue))
|
||||
now = datetime.datetime.now()
|
||||
log_filename = f"capture_{now.strftime('%Y-%m-%d_%H:%M:%S')}.txt"
|
||||
handler_task = asyncio.create_task(queue_handler(queue, log_filename=log_filename))
|
||||
|
||||
await asyncio.gather(server_task, client_task)
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user