-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPictClient.py
More file actions
187 lines (126 loc) · 4.5 KB
/
Copy pathPictClient.py
File metadata and controls
187 lines (126 loc) · 4.5 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
### This is a very basic implementation to ensure the User/SQL queries are handled correctly
### Prior to incorparating the GUI in PyGame
import socket, socketserver, threading, sys, time, ShootPyGame, builtins
from getpass import getuser
max_size = 1024
#connected.loggedin = False
#connected.failed = False
##############################################################################################################
class ClientConnection(threading.Thread):
"""This creates a thread off which a client can recieve data from the server"""
def __init__(self, handler, host, port):
super().__init__()
self.daemon = True
self.host = host
self.port = port
self.handler = handler
self.running = True
def run(self):
global sock
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((self.host, self.port))
self.handler.connected = True
except socket.error as error:
sock.close()
self.handler.connected = False
return
while self.running:
try:
incoming_data = sock.recv(max_size)
if self.handler.loggedin == False:
decoded_data = incoming_data.decode('utf-8').split("^")
if decoded_data[0] == "*loggEdin*":
self.randomint = decoded_data[1]
self.handler.loggedin = True
break
elif decoded_data[0] == "*faiLed*":
self.handler.failed = True
print("Sad Face")
elif decoded_data[0] == "***UsernAmeuSed***":
print("username used")
elif decoded_data[0] == "***EmailtAken***":
print("email used...")
except socket.error as error:
self.handler.warn_exit()
print("Done")
return
#Sending data,
def send_data(self, data):
"""Function that sends the clients data"""
try:
if len(data) == 0:
print("You need to type something to send!")
elif len(data) >= 1000:
print("You have exceeded the maximum message size")
else:
encoded_data = data.encode("utf-8")
sock.send(encoded_data)
except socket.error as error:
sys.exit("An error has occured, please try to connect to the host again, ERROR: {}".format(error))
class TCPConnection(threading.Thread):
"""This creates a thread off which a client can recieve data from the server"""
def __init__(self, master, randomint, host, port):
super().__init__()
self.daemon = True
self.host = host
self.port = port
self.randomint = randomint
self.start()
self.zero_count = 0
self.master = master
def run(self):
global sock
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((self.host, self.port))
recon_pack = "RECON^{}".format(self.randomint).encode('utf8')
sock.send(recon_pack)
except socket.error as error:
sock.close()
sys.exit("An error has occured, please try to connect to the host again, ERROR: {}".format(error))
return
while True:
try:
incoming_data = sock.recv(max_size)
print(len(incoming_data))
decoded_data = incoming_data.decode('utf-8')
if len(decoded_data) == 0:
self.zero_count += 1
if self.zero_count >=30:
self.master.running = False
print("!!!!!!!!Shut this shit DOWWWWWN!")
sys.exit("An error has occured, please try to connect to the host again")
elif decoded_data == "***ShUtdOwn***":
print("Shut this shit DOWWWWWN!")
elif decoded_data.startswith("DC"):
split_data = decoded_data.split("^")
randomint = int(split_data[1])
builtins.arrows.remove(builtins.arrow_dict[randomint])
else:
print(incoming_data.decode('utf-8'))
except socket.error as error:
sys.exit("An error has occured, please try to connect to the host again, ERROR: {}".format(error))
class ClientPinger(threading.Thread):
def __init__(self, host, port, randomint):
super().__init__()
self.daemon = True
self.host = host
self.port = port
self.randomint = randomint
self.start()
def run(self):
try:
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.socket.connect((self.host, self.port))
except socket.error as error:
sock.close()
sys.exit("An error has occured, please try to connect to the host again, ERROR: {}".format(error))
return
ping_packet = "PING^{}".format(self.randomint).encode("utf8")
while True:
try:
time.sleep(2)
self.socket.send(ping_packet)
except socket.error as error:
sys.exit("An error has occured, please try to connect to the host again, ERROR: {}".format(error))