-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWRedLogger.py
More file actions
executable file
·70 lines (55 loc) · 1.82 KB
/
Copy pathWRedLogger.py
File metadata and controls
executable file
·70 lines (55 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/python3
import traceback
import sys
import socketserver
from datetime import datetime
import time
import os
import shutil
# https://stackoverflow.com/a/57008707
# Context manager that copies stdout and any exceptions to a log file
class Tee(object):
def __init__(self, filename):
self.file = open(filename, 'w')
self.stdout = sys.stdout
def __enter__(self):
sys.stdout = self
def __exit__(self, exc_type, exc_value, tb):
sys.stdout = self.stdout
if exc_type is not None:
self.file.write(traceback.format_exc())
self.file.close()
def write(self, data):
self.file.write(data)
self.stdout.write(data)
self.flush()
def flush(self):
self.file.flush()
self.stdout.flush()
dirName = "./WRedLogs/"
logName = dirName + "WRedLog.txt"
if os.path.isfile(logName):
bakName = logName + "-" + str(int(time.time())) + ".txt"
shutil.copy2(logName, bakName)
sys.stdout = Tee(logName)
class MyTCPClientHandler(socketserver.StreamRequestHandler):
timeout = 1800
def handle(self):
print()
print("Connection established on", datetime.now().strftime("%Y-%m-%d %H:%M:%S"), "from", self.client_address)
outEnd = 10 # ord("\n")
while True:
data = self.rfile.read1()
if not data:
break
print(data.decode(errors="ignore"), end="")
outEnd = data[-1]
if outEnd != 10:
# Print missing newline
print()
print("Disconnected")
# https://pythontic.com/socketserver/threadingtcpserver/introduction
socketserver.ForkingTCPServer.allow_reuse_address = True
TCPServerInstance = socketserver.ForkingTCPServer(("0.0.0.0", 420), MyTCPClientHandler)
print("WRedLogger started")
TCPServerInstance.serve_forever()