-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclientNW.py
More file actions
90 lines (65 loc) · 2.04 KB
/
Copy pathclientNW.py
File metadata and controls
90 lines (65 loc) · 2.04 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
#!/usr/bin/env python3
import argparse
import random
import signal
import errno
import json
import os
from socket import *
from libraries.signalHandler import SignalHandler
from libraries.socket import ClientSocket
def printTicket(ticket):
with open("LotteryTickets.json", "a") as writeToFile:
json.dump(ticket, writeToFile)
print("Id:", "".join(ticket["id"]))
print(ticket["typeOfTicket"], "\n")
if ticket["typeOfTicket"] == "Lotto Max":
for i in ticket["numbers"]:
print(*i, sep="\n")
print("\n")
else:
print(*ticket["numbers"], sep="\n\n")
print("\n")
def main(maxConnections, maxClients):
ip = "localhost"
port = 8080
sockets = []
for clientNumber in range(maxClients):
try:
pid = os.fork()
except OSError:
sys.stderr.write("Could not fork() a child")
continue
if pid == 0:
for connectionNumber in range(maxConnections):
clientSocket = ClientSocket(ip, port)
clientSocket.sendData()
sockets.append(clientSocket)
dataRecived = clientSocket.receiveData()
print(
f"Client# {clientNumber + 1 } : Connection# {connectionNumber + 1}")
printTicket(dataRecived)
clientSocket.closeConnection()
signal.signal(signal.SIGCHLD, SignalHandler.childSignalHandler)
if __name__ == '__main__':
argParser = argparse.ArgumentParser()
argParser.add_argument(
"-c",
"--maxConections",
type=int,
nargs="?",
default=3,
const=3,
help="Maximum amount of connections (defualt 25).",
dest="maxConnections")
argParser.add_argument(
"-n",
"--maxClients",
type=int,
nargs="?",
default=1,
const=1,
help="Maximum amount of clients (default 1).",
dest="maxClients")
switches = argParser.parse_args()
main(switches.maxConnections, switches.maxClients)