-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.py
176 lines (118 loc) · 3.66 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
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
import sys
import socket
import select
import re
from utils import *
RECV_BUFFER = MESSAGE_LENGTH
class Client(object):
def __init__(self, chatname, server_host, server_port):
self.server_host = server_host
self.server_port = server_port
self.socket_list = []
# current channel name
self.in_channel = 'Fakechannel'
# self.channel_lst = []
self.chatname = chatname
self.buffering = False
self.split_buffer = ''
def serve(self):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(2)
# connect to remote host
try :
s.connect((host, port))
except :
print CLIENT_CANNOT_CONNECT.format(host, port)
sys.exit()
# to be removed
# print 'Connected to remote host. You can start sending messages'
sys.stdout.write('[Me] '); sys.stdout.flush()
first_msg = True
while 1:
socket_list = [sys.stdin, s]
# Get the list sockets which are readable
ready_to_read,ready_to_write,in_error = select.select(socket_list , [], [])
for sock in ready_to_read:
if sock == s:
# incoming message from remote server, s
data = sock.recv(RECV_BUFFER)
# TODO split messages handling in client or not???
# print "----> length of data is {}".format(len(data))
# if len(data) < 200 and not self.buffering:
# print "incomplete msg ...."
# self.split_buffer = data
# self.buffering = True
# continue
# if self.buffering:
# print "buffering ...."
# self.split_buffer += data
# if len(self.split_buffer) == 200:
# data = self.split_buffer
# self.split_buffer = ''
# self.buffering = False
# else:
# continue
# print ("-----DATA--->{}".format(data))
if not data :
print CLIENT_SERVER_DISCONNECTED.format(host, port)
sys.exit()
else :
#print data
sys.stdout.write(CLIENT_WIPE_ME + "\r"+data.rstrip(' '))
sys.stdout.write(CLIENT_MESSAGE_PREFIX); sys.stdout.flush()
else :
if first_msg:
msg = "{}".format(self.chatname)
s.send(pad_msg(msg))
first_msg = False
# user entered a message
msg = sys.stdin.readline()
# See if msg is a command
if re.search('^/', msg):
self.handle_cmd(msg, s)
# send msg if not a command
else:
s.send(pad_msg(msg))
sys.stdout.write(CLIENT_MESSAGE_PREFIX); sys.stdout.flush()
# command handler
def handle_cmd(self, cmd, server):
if re.search('^/join', cmd):
if re.search( '^/join .+', cmd):
channel = cmd.split()[1]
self.join_channel(channel, server)
else:
print SERVER_JOIN_REQUIRES_ARGUMENT
elif re.search('^/create', cmd):
if re.search( '^/create .+', cmd):
channel = cmd.split()[1]
self.create_channel(channel, server)
else:
print SERVER_CREATE_REQUIRES_ARGUMENT
elif re.search('^/list *', cmd):
self.list_channels(server)
else:
print SERVER_INVALID_CONTROL_MESSAGE.format(cmd)
# joining channel
def join_channel(self, channel, server):
server.send(pad_msg("/join " + channel))
# create channel
def create_channel(self, channel, server):
server.send(pad_msg("/create " + channel))
def list_channels(self, server):
server.send(pad_msg("/list"))
# for c in self.channel_lst:
# print "{}\n".format(c)
# helper functions
def pad_msg(msg):
if (len(msg) < MESSAGE_LENGTH):
msg = msg.ljust(MESSAGE_LENGTH)
return msg
if __name__ == "__main__":
if(len(sys.argv) < 4) :
print 'Usage : python client.py chatname hostname port'
sys.exit()
name = sys.argv[1]
host = sys.argv[2]
port = int(sys.argv[3])
client_server = Client(name, host, port)
sys.exit(client_server.serve())