-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpredictLotto.py
More file actions
168 lines (118 loc) · 3.63 KB
/
Copy pathpredictLotto.py
File metadata and controls
168 lines (118 loc) · 3.63 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#!/usr/bin/env python3
import logzero
import signal
import atexit
import errno
import json
import sys
import os
from socket import *
from logzero import logger
from libraries.Ticket import *
def handleClient(connectionSocket):
dataRecived = eval(connectionSocket.recv(1000).decode())
ticket = Lotto649()
ticketType = "Lotto 6/49"
dataToSend = dict(typeOfTicket=ticketType, numbers=[])
dataToSend["numbers"].append(ticket.generateNumbers())
connectionSocket.sendall(str(dataToSend).encode())
return dataToSend
def zombieSlayer(signalNumber, frame):
while True:
try:
pid, status = os.waitpid(-1, os.WNOHANG)
except OSError:
return
if pid == 0:
return
def terminator(signalNumber, frame):
raise SystemExit(1)
def daemonize(
pidfile,
*,
stdin="/dev/null",
stdout="/dev/null",
stderr="/dev/null"):
if os.path.exists(pidfile):
raise RuntimeError("Already running")
try:
if os.fork() > 0:
raise SystemExit(0)
except OSError as e:
raise RuntimeError("fork #1 failed.")
os.chdir("/")
os.umask(0)
os.setsid()
try:
if os.fork() > 0:
raise SystemExit(0)
except OSError as e:
raise RuntimeError("fork #2 failed.")
sys.stdout.flush()
sys.stderr.flush()
with open(stdin, "rb", 0) as f:
os.dup2(f.fileno(), sys.stdin.fileno())
with open(stdout, "ab", 0) as f:
os.dup2(f.fileno(), sys.stdout.fileno())
with open(stderr, "ab", 0) as f:
os.dup2(f.fileno(), sys.stderr.fileno())
with open(pidfile, "w") as f:
print(os.getpid(), file=f)
atexit.register(lambda: os.remove(pidfile))
signal.signal(signal.SIGTERM, terminator)
def main():
ip = "localhost"
port = 8080
numberOfClients = 1000
serverSocket = socket(AF_INET6, SOCK_STREAM)
serverSocket.bind((ip, port))
serverSocket.setsockopt(SOL_SOCKET, SO_REUSEPORT, 1)
serverSocket.listen(numberOfClients)
signal.signal(signal.SIGCHLD, zombieSlayer)
while True:
connectionSocket, address = serverSocket.accept()
try:
pid = os.fork()
except OSError:
sys.stderr.write("Could not fork in main")
logger.error("Could not fork in main")
continue
if pid == 0:
serverSocket.close()
dataSent = handleClient(connectionSocket)
connectionSocket.close()
os._exit(0)
else:
logger.info(f"PARENT PID: {os.getpid()}")
connectionSocket.close()
if __name__ == "__main__":
PIDFILE = "/tmp/daemon.pid"
logzero.logfile(
"/tmp/rotating-logfile.log",
maxBytes=1e6,
backupCount=3,
disableStderrLogger=True)
if len(sys.argv) != 2:
print(f"Usage: {sys.argv[0]} [start|stop]", file=sys.stderr)
raise SystemExit(1)
if sys.argv[1] == "start":
try:
daemonize(
PIDFILE,
stdout="/tmp/daemon.log",
stderr="/tmp/dameon.log")
except RuntimeError as e:
print(e, file=sys.stderr)
raise SystemExit(1)
logger.info(f"Started {os.getpid()}")
main()
elif sys.argv[1] == "stop":
if os.path.exists(PIDFILE):
with open(PIDFILE) as f:
os.kill(int(f.read()), signal.SIGTERM)
else:
print("Not running", file=sys.stderr)
raise SystemExit(1)
else:
print(f"Unknown command {sys.argv[1] !r}", file=sys.stderr)
raise SystemExit(1)