def start_server_listener(the_host_name=HOST_NAME, the_max_clients=MAX_CLIENTS): ''' Starts the Secure Server, if successfully also starts listening or a client/chat partner to connect. ''' try: secure_utils.clear_screen() # clear screen for nice output secure_utils.draw_server_logo() # draws logo my_server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) my_server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) my_server_socket.bind(('', PORT_NUMBER)) my_server_socket.listen(the_max_clients) print "\033[1;43m[+] The Chat Server Successfully Started\033[1;m" print "\033[1;43m[+] Now accepting chat clients\033[1;m\n" # Blocking call to accept connections (my_client_connection, my_client_address) = my_server_socket.accept() print "[+] User {0} at {1} is now connected to the server".format(socket.gethostname(), my_client_address[0]) if (my_client_address[0] in secure_utils.populated_banned_list()): # check if this user is banned from connecting, send appropriate msg print "[+] User at {0} is banned from this server... disconnecting".format(my_client_address[0]) my_client_connection.send("Its unfortunate, but you are banned from connecting to this server") return 0 else: my_client_connection.send("Send Keys") # Tell other party to send their public keys. # wait to receive client's public key (publicKeyTuple, privateKeyTuple) = exchange_keys_with_client(my_client_connection) print '\n\033[1;42m*******Type your message below and hit enter to send. Type \'EXIT\' to end conversation.*******\033[1;m\n' ReadAndDecryptThread = ThreadManager('read', my_client_connection, publicKeyTuple, privateKeyTuple) # Thread for reading data and decrypting WriteAndEcnryptThread = ThreadManager('write', my_client_connection, publicKeyTuple, privateKeyTuple) # Thread for writing data and decrypting ReadAndDecryptThread.start() # Start the read and decrypt thread WriteAndEcnryptThread.start() # Starts the writing and encrypting thread # wait until client dc's ReadAndDecryptThread.join() print '[+] Your partner has left the conversation. Press any key to continue...\n' # stop the write thread WriteAndEcnryptThread.stop_write_loop() WriteAndEcnryptThread.join() # shut down client connection try: my_client_connection.shutdown(socket.SHUT_RDWR) my_client_connection.close() except: # connection already closed pass # shut down server secure_utils.display_processing_cursor('Shutting server down...') my_server_socket.shutdown(socket.SHUT_RDWR) my_server_socket.close() return 0 except socket.error as socket_error: print socket_error[1] raw_input("\nPress [ENTER] To Return To The Main Menu...\n") # print "[-] There was problem starting the server" pass