-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.py
102 lines (71 loc) · 2.14 KB
/
client.py
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
#!/usr/bin/env python3
import argparse
import random
import signal
import errno
import json
import os
from socket import *
def printTicket(ticket):
with open("prediction.json", "a") as writeToFile:
json.dump(ticket, writeToFile)
print(*ticket["numbers"], sep="\n\n")
print("\n")
def sendRequest(clientSocket):
typeOfTicket = 2
numberOfTickets = 1
dataToSend = dict(
typeOfTicket=typeOfTicket,
numberOfTickets=numberOfTickets)
clientSocket.sendall(str(dataToSend).encode())
def zombieSlayer(signalNumber, frame):
while True:
try:
pid, status = os.waitpid(-1, os.WNOHANG)
except OSError:
return
if pid == 0:
return
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 = socket(AF_INET6, SOCK_STREAM)
clientSocket.connect((ip, port))
sendRequest(clientSocket)
sockets.append(clientSocket)
dataRecived = eval(clientSocket.recv(1000).decode())
print(f"\nHere is your prediction for this weeks Lotto 6/49\n")
printTicket(dataRecived)
os._exit(0)
signal.signal(signal.SIGCHLD, zombieSlayer)
if __name__ == '__main__':
argParser = argparse.ArgumentParser()
argParser.add_argument(
"-c",
"--maxConections",
type=int,
nargs="?",
default=1,
const=1,
help="Maximum amount of connections (defualt 1).",
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)