-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_client.py
102 lines (93 loc) · 3.64 KB
/
test_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
# import client
from server import *
import pytest
def testConn():
listenSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print("ok")
listenSocket.bind(('localhost', int(8000)))
listenSocket.listen(10)
assert listener(listenSocket, True) == True
assert listener(listenSocket, False) == True
def listener(listenSocket, b):
print("start listener")
while True:
client,_ = listenSocket.accept()
if handleConnection(client, b) == 1:
client.send(b"/disconnect")
return True
else:
client.send(b"/disconnect")
return False
def handleConnection(client, b):
print("start handleConnection")
try:
username = client.recv(1024).decode("utf-8")
if username == "/emergencyDisconnect":
return 0
# print("hc2")
client.send(b"/sendGroupname")
groupname = client.recv(1024).decode("utf-8")
except Exception:
return 0
if groupname == "/emergencyDisconnect":
return 0
if b:
conn1 = connect(client, username, True)
return 1
else:
conn2 = connect(client, username, False)
return 1
# if conn1 and conn2:
# print("done final")
# return 1
# print("hc3")
# if groupname in self.groups:
# # if username in groups[groupname].allMembers:
# if self.groups[groupname].connect(client, username) == 1:
# print("connected, user already a member")
# else:
# print("user waiting for approval")
# else:
# self.groups[groupname] = Group(username, client, groupname)
# self.groups[groupname].threads[username] = threading.Thread(target=self.waitUserInput, args=(client, username, groupname), daemon=True).start()
# client.send(b"/adminReady")
# print("New Group:",groupname,"| Admin:",username)
# print("handleConnection FINISHED")
def connect(client, username, allMembers):
print("do connect")
try:
if allMembers:
print("in allMembers")
# self.onlineMembers.add(username)
# self.clients[username].recv(1024)
print("send /accepted")
client.send(b"/accepted")
print("connect from user ",client.recv(1024))#.decode("utf-8")
printMessageHistory(username, client)
client.send(b"/sendCommands")
client.recv(1024)
# if username == self.admin:
client.send(b"/1(Requests), /2(Accept Request), /3(Disconnect), \n/4(All Members), /5(Online Members), /8(Kick Member)")
# else:
# client.send(b"/3(Disconnect), /4(All Members), /5(Online Members)")
client.recv(1024)
print("USER CONNECTED:",username,"| Group:", "groupname")
return True
else:
print("not in allMembers")
# self.onlineMembers.add(username)
# self.joinRequests.add(username)
# self.waitClients[username] = client
# self.sendMessage(username+" has requested to join the group.", self.admin)
client.send(b"/wait")
print("Join Request:",username,"| Group:","groupname")
print("finish connect non member ",client.recv(1024))
return True
except Exception:
return False
def printMessageHistory(username, client):
client.send(b"/loadMessageHistory")
client.recv(1024)
client.send(pickle.dumps([{"username": "ushis", "message":"mhis"}]))
client.recv(1024)
testConn()