Skip to content

Commit 677cb45

Browse files
committed
cosmetic changes
1 parent 1107467 commit 677cb45

File tree

2 files changed

+59
-47
lines changed

2 files changed

+59
-47
lines changed

client.py

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -47,36 +47,41 @@ def receive_data(conn):
4747
data_identifiers = {'info': 0, 'data': 1, 'image': 2}
4848
# key to be trusted by server
4949
key_message = 'C0nn3c+10n'
50+
# a sample dictionary data
51+
data = {'data number': 0,
52+
'message': 'A new message has been arrived from client'}
5053

5154

5255
def main():
5356
# create client socket object and connect it to server
5457
conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
5558
conn.connect(('127.0.0.1', 12345))
5659
send_data(conn, key_message)
57-
58-
# send a dictionary and image data in loop till keyboard interrupt is received
59-
data = {'data number': 0,
60-
'message': 'A new message has been arrived from client'}
61-
while True:
62-
try:
63-
# send dict
64-
data['data number'] += 1
65-
send_data(conn, data, data_identifiers['data'])
66-
print(receive_data(conn)[1])
67-
# send image
68-
image = cv2.imread('client_data/sample_image.png', 0)
69-
send_data(conn, image, data_identifiers['image'])
70-
print(receive_data(conn)[1])
71-
except KeyboardInterrupt:
72-
print(receive_data(conn)[1])
73-
print('\n---Keyboard Interrupt received---')
74-
break
75-
# once keyboard interrupt is received, send signal to serer for closing connection
76-
# and close client socket
77-
send_data(conn, 'bye')
78-
print(receive_data(conn)[1])
60+
first_payload = receive_data(conn)[1]
61+
if first_payload == 'You are not authorized':
62+
print('[ERROR]: Access denied')
63+
else:
64+
# send a dictionary data and image data in loop till keyboard interrupt is received
65+
while True:
66+
try:
67+
# send dict
68+
data['data number'] += 1
69+
send_data(conn, data, data_identifiers['data'])
70+
print(receive_data(conn)[1])
71+
# send image
72+
image = cv2.imread('client_data/sample_image.png', 0)
73+
send_data(conn, image, data_identifiers['image'])
74+
print(receive_data(conn)[1])
75+
except KeyboardInterrupt:
76+
print(receive_data(conn)[1])
77+
print('\n[INFO]: Keyboard Interrupt received')
78+
break
79+
# once keyboard interrupt is received, send signal to server for closing connection
80+
send_data(conn, 'bye')
81+
print(receive_data(conn)[1])
82+
# close connection
7983
conn.close()
84+
print('[INFO]: Connection closed')
8085

8186

8287
if __name__ == '__main__':

server.py

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -68,25 +68,29 @@ def handle_client(conn, conn_name):
6868
con_name: name of the connection
6969
'''
7070
while True:
71-
data_id, payload = receive_data(conn)
72-
# if data identifier is image then save the image
73-
if data_id == data_identifiers['image']:
74-
print('---Recieved image too ---')
75-
cv2.imwrite('server_data/received_image.png', payload)
76-
send_data(conn, 'Image received on server')
77-
# otherwise send the data to do something
78-
elif data_id == data_identifiers['data']:
79-
response = do_something(conn_name, payload)
80-
send_data(conn, response)
81-
else:
82-
# if data is 'bye' then break the loop and client connection will be closed
83-
if payload == 'bye':
84-
print('{} requested to close the connection'.format(conn_name))
85-
print('Closing connection with {}'. format(conn_name))
86-
send_data(conn, 'You are disconnected from server now')
87-
break
71+
try:
72+
data_id, payload = receive_data(conn)
73+
# if data identifier is image then save the image
74+
if data_id == data_identifiers['image']:
75+
print('---Recieved image too ---')
76+
cv2.imwrite('server_data/received_image.png', payload)
77+
send_data(conn, 'Image received on server')
78+
# otherwise send the data to do something
79+
elif data_id == data_identifiers['data']:
80+
response = do_something(conn_name, payload)
81+
send_data(conn, response)
8882
else:
89-
print(payload)
83+
# if data is 'bye' then break the loop and client connection will be closed
84+
if payload == 'bye':
85+
print(
86+
'[INFO]: {} requested to close the connection'.format(conn_name))
87+
print('[INFO]: Closing connection with {}'. format(conn_name))
88+
send_data(conn, 'You are disconnected from server now')
89+
break
90+
else:
91+
print(payload)
92+
except:
93+
print('[WARNING]: There was some issue with connection')
9094
conn.close()
9195

9296

@@ -102,7 +106,7 @@ def main():
102106
port = 12345
103107
server_socket.bind(('127.0.0.1', port))
104108
server_socket.listen(5)
105-
print('---Server Started---')
109+
print('[INFO]: Server Started')
106110

107111
while True:
108112
try:
@@ -112,22 +116,25 @@ def main():
112116
# otherwise close the connection
113117
conn, (address, port) = server_socket.accept()
114118
conn_name = '{}|{}'.format(address, port)
115-
print("Accepted the connection from {}".format(conn_name))
116-
_, first_payload = receive_data(conn)
119+
print("[INFO]: Accepted the connection from {}".format(conn_name))
120+
first_payload = receive_data(conn)[1]
117121
if first_payload == key_message:
118-
print('Connection could be trusted, begining communication')
122+
print('Connection could be trusted, rejecting communication')
123+
send_data(conn, 'Connection accepted')
119124
threading.Thread(target=handle_client,
120125
args=(conn, conn_name)).start()
121126
else:
122-
print('Accepted connection is an unknown client, closing the connection')
127+
print('[WARNING]: Accepted connection is an unknown client, \
128+
closing the connection')
129+
send_data(conn, 'You are not authorized')
123130
conn.close()
124131
# break the while loop when keyboard intterupt is received and server will be closed
125132
except KeyboardInterrupt:
126-
print('\n---Keyboard Interrupt Received---')
133+
print('\n[INFO]: Keyboard Interrupt Received')
127134
break
128135

129136
server_socket.close()
130-
print('---Server Closed---')
137+
print('[INFO]: Server Closed')
131138

132139

133140
if __name__ == '__main__':

0 commit comments

Comments
 (0)