@@ -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
133140if __name__ == '__main__' :
0 commit comments