-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.py
254 lines (204 loc) · 10.7 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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
"""UDP communicator provides client-server communication with file transfer based on UDP.
School assignment n. 2 from course Computer and Communication Networks.
"""
import server
import protocol
import select
import socket
import sys
import os
import ntpath
import math
import time
__author__ = "Erik Matovic"
__version__ = "1.0.1"
__email__ = "[email protected]"
__status__ = "Production"
def get_file_name(file_path):
""" Get file name from file path. Returns name of a file.
file_path: Given path from user to file to be transferred. """
return ntpath.split(file_path.decode('utf-8'))[1]
def initialization(client_socket, server_ip, server_port, fragment_size, data):
""" Text or file transfer initialization.
client_socket: Client socket contains source address and sendto method.
server_ip: One part of a target address in socket
server_port: Second part of a target address in socket
fragment_size: Maximum size of one fragment given by user
data: Data of text transfer contains one byte telling protocol to add msg type as text transfer.
Data of file transfer contains name of a file to be created on a server.
Rest is number of fragments both for text transfer and file transfer. """
try:
while True:
client_socket.sendto( # initialization connection
protocol.msg_initialization(fragment_size, data), # send info about file
(server_ip, server_port)) # server address
ready = select.select([client_socket], [], [], 5)
if ready[0]:
new_data, server_address = client_socket.recvfrom(fragment_size)
else:
print('Connection not established')
return 0
if new_data[:1].decode('utf-8') == protocol.MsgType.ACK.value:
break
print(protocol.MsgReply.SET.value)
except ConnectionResetError:
print('Connection lost. Turn server on.')
return 1
def send_file(server_ip, client_socket, server_port, fragment_size, file_path):
""" File transfer.
client_socket: Client socket contains source address and sendto method.
server_ip: One part of a target address in socket
server_port: Second part of a target address in socket
fragment_size: Maximum size of one fragment given by user
file_path: Containt file to be open for reading and fo data bytes tranfers. """
user_input_mistake = input('Make a mistake in transfer? y/n ')
if user_input_mistake.lower() != 'y' and user_input_mistake.lower() != 'n':
print('Wrong input!')
send_file(server_ip, client_socket, server_port, fragment_size, file_path)
file_name = bytes(get_file_name(file_path), 'utf-8')
file_size = os.path.getsize(file_path)
num_of_fragment = math.ceil(file_size / (fragment_size - protocol.HEADER_SIZE))
nack_fragment, all_fragment = 0, 0 # doimplementacia
try:
if initialization(client_socket, server_ip, server_port, fragment_size,
file_name + bytes(str(num_of_fragment), 'utf-8')) == 0:
return
start_time = time.time()
with open(file_path, 'rb+') as file:
data = file.read(fragment_size - protocol.HEADER_SIZE)
fragment_count = 0
while data:
new_data = protocol.add_header(protocol.MsgType.PSH, fragment_size, data)
if user_input_mistake == 'y':
header = new_data[:6]
new_data = header + new_data[20:]
user_input_mistake = 'n'
if client_socket.sendto(new_data, (server_ip, server_port)):
reply, server_address = client_socket.recvfrom(fragment_size)
if reply[:1].decode('utf-8') != protocol.MsgType.ACK.value: # ARQ Stop & Wait
print('negative acknowledgment msg')
nack_fragment += 1 # doimplementacia
all_fragment += 1 # doimplementacia
else:
data = file.read(fragment_size - protocol.HEADER_SIZE)
fragment_count += 1
all_fragment += 1 # doimplementacia
ready = select.select([client_socket], [], [], 5)
if ready[0]:
if client_socket.recvfrom(fragment_size):
end_time = time.time()
print(protocol.MsgReply.ACK.value)
print('Time:', end_time - start_time)
print('File at', os.path.abspath(file_path.decode('utf-8')))
print('Send fragments are', fragment_count, 'and counted fragments are', num_of_fragment, '\n')
# doimplementacia
print('Send fragments are', all_fragment, 'and nack fragments are ', nack_fragment, '\n')
else:
print('Connection not established')
return
except ConnectionResetError:
print('Connection lost. Turn server on.')
def send_message(server_ip, client_socket, server_port, fragment_size, message):
""" Text message transfer.
client_socket: Client socket contains source address and sendto method.
server_ip: One part of a target address in socket
server_port: Second part of a target address in socket
fragment_size: Maximum size of one fragment given by user
Message: Data to be transferred. """
fragment_count = 0
num_of_fragment = math.ceil(len(message.decode('utf-8')) / fragment_size)
nack_fragment, all_fragment = 0, 0 # doimplementacia
try:
if initialization(client_socket, server_ip, server_port, fragment_size + protocol.HEADER_SIZE,
bytes(protocol.MsgType.SET_MSG.value, 'utf-8') + bytes(str(num_of_fragment), 'utf-8')) == 0:
return
lenght = math.ceil(len(message) / fragment_size)
index1, index2 = 0, fragment_size
while lenght > 0:
data = protocol.add_header(protocol.MsgType.PSH, fragment_size, message[index1:index2])
client_socket.sendto(data, (server_ip, server_port))
ready = select.select([client_socket], [], [], 5)
if ready[0]:
reply, server_address = client_socket.recvfrom(fragment_size + protocol.HEADER_SIZE)
if reply[:1].decode('utf-8') != protocol.MsgType.ACK.value: # ARQ Stop & Wait
print('negative acknowledgment msg')
nack_fragment += 1 # doimplementacia
all_fragment += 1 # doimplementacia
else:
index1 += fragment_size
index2 += fragment_size
lenght -= 1
fragment_count += 1
all_fragment += 1 # doimplementacia
else:
print('Connection not established')
return
except ConnectionResetError:
print('Connection lost. Turn server on.')
print('Send fragments are', fragment_count, 'and counted fragments are', num_of_fragment, '\n')
# doimplementacia
print('Send fragments are', all_fragment, 'and nack fragments are ', nack_fragment, '\n')
def set_client():
""" Initial client settings. Returns server_ip, client_port, server_port, fragmentation entered by user. """
server_ip, client_port, server_port, fragmentation = '', '', '', ''
while server_ip == '' or server_port == '' or fragmentation == '': # read values to set client
try:
if server_ip == '':
server_ip = input('Enter server IP address: ')
socket.inet_aton(server_ip) # validate given IP address
if client_port == '':
client_port = int(input('Enter client/source port: '))
if client_port < 1024 or client_port > 65535: # validate given port
print('You must enter non - privileged port!')
client_port = ''
continue
if server_port == '':
server_port = int(input('Enter server port: '))
if server_port < 1024 or server_port > 65535: # validate given port
print('You must enter non - privileged port!')
server_port = ''
continue
fragmentation = int(input('Enter max size of fragment: '))
if fragmentation < protocol.FRAGMENT_MIN: # validate given value of fragment
fragmentation = protocol.FRAGMENT_MIN
if fragmentation > protocol.FRAGMENT_MAX: # validate max fragment
fragmentation = protocol.FRAGMENT_MAX
except ValueError: # wrong data type entered
print('Wrong input! Try again')
server_ip, server_port, fragmentation = '', '', ''
continue
except OSError: # wrong IP address entered
print('Wrong IP address! Try again')
server_ip = ''
continue
return server_ip, client_port, server_port, fragmentation
def user_interface():
""" Client user interface. """
print('\n{:^50}'.format('client'))
server_ip, client_port, server_port, fragmentation = set_client()
client_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # set socket with IPv4 and UPD
client_socket.bind(('', client_port)) # set source port
while True:
print('0 - end\n'
'1 - send message\n'
'2 - send file\n'
'9 - change to server')
user_input = input('Enter what do you want to do: ')
if user_input == '0':
print('End')
client_socket.close()
sys.exit(0)
if user_input == '1':
message = bytes(input('Enter message: '), 'utf-8')
send_message(server_ip, client_socket, server_port, fragmentation, message)
elif user_input == '2':
file = bytes(input('Enter path to the file: '), 'utf-8')
if not os.path.isfile(file): # check if given path is valid
print('ERROR 01: Path', file.decode('utf-8'), 'does not exist!')
continue
send_file(server_ip, client_socket, server_port, fragmentation + protocol.HEADER_SIZE, file)
elif user_input == '9':
client_socket.close()
server.user_interface()
else:
print('\n{:^30}'.format('Wrong input! Try again.\n'))