-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.py
220 lines (181 loc) · 6.03 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import sys
import threading
import socket
PORT = 8828
username = sys.argv[1]
SERVER_IP = sys.argv[2]
#Function for parsing input message by client
def input_parse(mess):
recp = ""
message =""
wrongform = False
if mess[0] == '@':
k = mess.find(':')
if k == -1:
wrongform = True
recp = mess[1:k]
message = mess[k+1:]
message = message.strip()
if recp == '' or message=='':
wrongform = True
else:
wrongform = True
return wrongform,recp,message
#Function for parsing forwarded message by server
def forward_parse(mess):
l = mess.split('\n')
l0 = l[0].split(' ')
typ = l0[0]
sender = l0[1]
l1 = l[1].split(' ')
cl = int(l1[1])
mess = l[3]
wrongform = False
if typ != 'FORWARD' or sender == '' or mess == '' or len(mess) != cl:
wrongform = True
return wrongform,sender,mess
#------------------------------------------------------------------------------------------#
'''
Function for execution in send socket thread
'''
def send(sock):
while True:
mess = input()
#parse input
wrongform,recp,message = input_parse(mess)
#if wrong format print help
if wrongform:
print('Use the following format for sending messages:')
print('@[RECIPIENT NAME]: Message\n')
continue
if recp == username:
print('Receiver and Username are same')
continue
#adding header to message
messagetosend = 'SEND {}\nContent-length: {}\n\n{}'.format(recp,len(message),message)
try:
sock.send(bytes(messagetosend,'utf-8'))
except:
print('Server Inactive')
break
#waiting for server's response
try:
server_mess = sock.recv(2048).decode()
except:
print('Server Inactive')
break
#parse message of server
if server_mess == 'SEND {}\n\n'.format(recp):
print('Message delivered to {}'.format(recp))
elif server_mess == 'ERROR 102 Unable to send\n\n':
print('ERROR 102 : Unable to Send : Message not delivered to {}'.format(recp))
elif server_mess == 'ERROR 103 Header Incomplete\n\n':
print('ERROR 103 : Incomplete Header : Message not delivered to {}'.format(recp))
print('Connection closed by server')
break
else:
print('Unknown Error')
break
sock.close()
#------------------------------------------------------------------------------------------#
'''
Function for execution in recv socket thread
'''
def recv(sock):
while True:
try:
mess = sock.recv(2048).decode()
except:
print('Server Inactive')
break
#parse the message forwarded by server
wform,sender,message = forward_parse(mess)
#send response to server depending on forwarded message
if wform:
response = 'ERROR 103 Header Incomplete\n\n'
else:
response = 'RECEIVED {}\n\n'.format(sender)
print('{}: {}'.format(sender,message))
#response sent to server
try:
sock.send(bytes(response,'utf-8'))
except:
print('Server Inactive')
break
sock.close()
#------------------------------------------------------------------------------------------#
if username == 'ALL':
print('ALL is reserved keyword, use any other username')
sys.exit()
#------------------------------------------------------------------------------------------#
'''
Send socket initialized and request for registration is sent
'''
send_client = socket.socket()
send_client.connect((SERVER_IP,PORT))
send_reg_message = 'REGISTER TOSEND {}\n\n'.format(username)
try:
send_client.send(bytes(send_reg_message,'utf-8'))
except:
print('Server Inactive')
sys.exit()
#if acknowledged by server continue else if incorrect username throw (ERROR100)
try:
send_ack_message = send_client.recv(2048).decode()
except:
print('No server response')
sys.exit()
#username is okay and send socket is registered
if send_ack_message == 'REGISTERED TOSEND {}\n\n'.format(username):
#thread for send socket started
print('{} : REGISTERED TOSEND'.format(username))
send_th = threading.Thread(target=send,args=(send_client,))
#send_th.start()
#username is malformed
elif send_ack_message == 'ERROR 100 Malformed username\n\n':
print('ERROR 100: Malformed Username')
print('Restart the application...')
print('Exiting...')
sys.exit()
#reply to any other query
else:
print('ERROR 101 : No user registered')
print('Exiting...')
sys.exit()
#------------------------------------------------------------------------------------------#
'''
Recv socket initialized and request for registration is sent
'''
recv_client = socket.socket()
recv_client.connect((sys.argv[2],PORT))
recv_reg_message = 'REGISTER TORECV {}\n\n'.format(username)
try:
recv_client.send(bytes(recv_reg_message,'utf-8'))
except:
print('Server is unavailable')
sys.exit()
#if acknowledged by server continue elseif incorrect username (ERROR100)
try:
recv_ack_message = recv_client.recv(2048).decode()
except:
print('No response from server')
#username is okay and recv socket is registered
if recv_ack_message =='REGISTERED TORECV {}\n\n'.format(username):
#thread for recv
print('{} : REGISTERED TORECV'.format(username))
recv_th = threading.Thread(target=recv,args=(recv_client,))
#recv_th.start()
#username is malformed
elif recv_ack_message == 'ERROR 100 Malformed username\n\n':
print('ERROR 100 : Malformed Username')
print('Restart the application...')
print('Exiting...')
sys.exit()
#reply to any other query
else:
print('ERROR 101')
print('Exiting...')
sys.exit()
#------------------------------------------------------------------------------------------#
send_th.start()
recv_th.start()