-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.py
61 lines (49 loc) · 1.64 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
# client.py
import socket
from _thread import *
class Client:
def __init__(self, ip, port, username, tab):
self.username = username
self.tab = tab
self.ip = ip
self.port = port
self.addr = (ip, port)
self.connected = False
self.client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.connect()
self.send('NEW|'+username)
start_new_thread(self.listen, ())
self.connected = True
def connect(self):
try:
self.client.connect(self.addr)
except Exception as e:
print(e)
def listen(self):
while True:
print('['+self.username+'] listening...')
try:
ret = self.client.recv(2048)
if not ret:
break
ret = ret.decode("utf-8")
print('['+self.username+']<< ', ret)
if ret[:9]=='CONNECTED':
ret = ret[9:]
if ret:
cmd, value = ret.split('|')
if cmd=='USERS':
self.tab.clearUsers()
for user in eval(value):
self.tab.addUser(user)
except socket.error as e:
print(e)
break
def send(self, data):
try:
print('['+self.username+']>> ', data)
self.client.send(str.encode(data))
except socket.error as e:
print(e)
def stop(self):
self.client.close()