-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClient.py
146 lines (132 loc) · 4.02 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
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
import socket,json,threading,errno,time
HOST = "localhost"
PORT = 10000
#Server
#s=socket.gethostname()
class Client:
def __init__(self,name):
self.name = name
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.socket.settimeout(2)
# self.socket.settimeout(0)
self.trhread = threading.Thread(target=self.recv)
self.loop = True
self.connect = False
self.data=None
self.gotdata=False
def __del__(self):
self.close()
def close(self):
try: self.socket.close()
except: pass
def conn(self,host, port):
try:
self.socket.connect((host, port))
self._setconn(True)
return True
except Exception as ex:
return False
def sendall(self,message):
try:
j = json.dumps({self.name: message}).encode('utf-8')
self.socket.sendall(j)
return True
except:
return False
def _setconn(self,state):
self.connect = state
def run(self):
if self.connect:
try:
self.loop = True
self.trhread.start()
return True
except Exception as ex:
print(ex)
return False
return False
def stop(self):
self.loop = False
def recv(self):
while self.loop:
# data = set()
data = bytearray()
while True:
try:
data+=self.socket.recv(1024)
except socket.timeout as e:
err = e.args[0]
if err == 'timed out':
if data:
break
else:
time.sleep(1)
self.gotdata = False
continue
# else:
# self._setconn(False)
# self.gotdata = self.data = False
# return False
# except socket.error as e:
# err = e.args[0]
# if err == errno.EAGAIN or err == errno.EWOULDBLOCK:
# if data:
# break
# else:
# time.sleep(5)
# self.gotdata=False
# continue
# else:
# self._setconn(False)
# self.gotdata=self.data = False
# return False
# except Exception as ex:
# self._setconn(False)
# self.data = False
# return False
self.data = data
self.gotdata = True
print(self.data)
time.sleep(5)
# def client(host,port,name='',message = ''):
# if not name: name = input('your name: ')
# if not message: message = input('your message: ')
# j = json.dumps({name:message}).encode('utf-8')
# #j='<EOF>'.encode('utf-8')
# with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
# s.connect((host, port))
# s.sendall(j)
# data = s.recv(1024)
# print('Received', repr(data.decode()))
#
#
# thread_list = []
# for x in range(1,100):
# t = threading.Thread(target=client,args=(HOST,PORT,f"client {x}",f'message {x}'))
# thread_list.append(t)
# t.start()
# [t.join() for t in thread_list]
clientList = []
for x in range(1,10):
c = Client(f'name {x}')
if not c.conn(HOST,PORT):
continue
if not c.run():
continue
clientList.append(c)
while True:
for i,c in enumerate(clientList):
c.sendall(f'message {i}')
# time.sleep(10)
time.sleep(10)
# time.sleep(30)
#
# for c in clientList:
# print('print data: ',c.data)
#
#
# print('ok')
# for c in clientList:
# c.close()
# for c in clientList:
# clientList.pop()