Advertisement
black12345

chat_client_version_1

Jan 9th, 2019
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.53 KB | None | 0 0
  1. import socket
  2. from time import sleep
  3. import random
  4. import threading
  5.  
  6. class Client:
  7.     def __init__(self):
  8.         self.host = '192.168.0.250'
  9.         self.port = 9000
  10.         self.account_options()
  11.  
  12.     def send_account_details(self, username, password):
  13.         server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  14.         try:
  15.             server.connect((self.host, self.port))
  16.             create_account_query = 'CRTACC'
  17.             server.sendall(str.encode(create_account_query))
  18.             sleep(1)
  19.             server.sendall(str.encode(username))
  20.             sleep(1)
  21.             server.sendall(str.encode(password))
  22.             while True:
  23.                 server_response = server.recv(1024)
  24.                 # print(server_response)
  25.                 if server_response:
  26.                     result = server_response.decode()
  27.                     break
  28.             if result == 'Account has been made!':
  29.                 print(result)
  30.                 self.account_options()
  31.         except:
  32.             print('Connection error, make sure the server is up and you are connected to the internet!')
  33.             self.account_options()
  34.  
  35.     def verify_account_details(self, username, password):
  36.         server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  37.         try:
  38.             server_socket.connect((self.host, self.port))
  39.             sign_in_query = 'SIGNIN'
  40.             server_socket.sendall(str.encode(sign_in_query))
  41.             sleep(1)
  42.             server_socket.sendall(str.encode(username))
  43.             sleep(1)
  44.             server_socket.sendall(str.encode(password))
  45.             while True:
  46.                 server_response = server_socket.recv(1024)
  47.                 if server_response:
  48.                     verification_result = server_response.decode()
  49.                     break
  50.             print(verification_result)
  51.             if verification_result == 'Password Confirmed!':
  52.                 self.main(username)
  53.             elif verification_result == 'Credential Error!':
  54.                 self.account_options()
  55.         except:
  56.             print('Connection error, make sure the server is up and you are connected to the internet!')
  57.             self.account_options()
  58.            
  59.     def account_options(self):
  60.         print('Type in 1 to create a new account! \n')
  61.         print('Type in 2 to sign into an existing account! \n')
  62.         print('Type in 3 to exit program!')
  63.         user_choice = input()
  64.         if user_choice == '1':
  65.             username = input('Username: ')
  66.             if len(username) < 4 or len(username) > 12:
  67.                 print('Your username cannot be less than 4 characters and cannot be longer than 12 characters!')
  68.                 self.create_account()
  69.             else:
  70.                 username = username.replace(' ','')
  71.                 print('If you password has spaces they will be removed! \n')
  72.                 password = input('Password: ')
  73.                 password = password.replace(' ', '')
  74.                 self.send_account_details(username, password)
  75.         elif user_choice == '2':
  76.             username = input('Username: ')
  77.             password = input('Password: ')
  78.             self.verify_account_details(username, password)
  79.         elif user_choice == '3':
  80.             sys.exit()
  81.         else:
  82.             print('Invalid command! Restarting...')
  83.             self.account_options()
  84.  
  85.     def get_chat_history(self, username, chat_log_to_check):
  86.         server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  87.         server.connect((self.host, self.port))
  88.         get_chat_history_query = 'GETCHATHISTORY'
  89.         server.sendall(str.encode(get_chat_history_query))
  90.         sleep(1)
  91.         server.sendall(str.encode(username))
  92.         sleep(1)
  93.         server.sendall(str.encode(chat_log_to_check))
  94.         while True:
  95.             chat_history = server.recv(1024)
  96.             if chat_history:
  97.                 print(chat_history.decode())
  98.                 break
  99.         self.main(username)
  100.    
  101.     def main(self, username):
  102.         server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  103.         print('Type in 1 to send a message to another user!')
  104.         print('Type in 2 to check your messages!')
  105.         print('Type in 3 to log out!')
  106.         user_choice = input()
  107.         if user_choice == '1':
  108.             try:
  109.                 print('Enter the other person\'s username!')
  110.                 to_user = input('Username: ')
  111.                 to_user = to_user.strip(' ')
  112.                 message = input('Message: ')
  113.                 server_socket.connect((self.host, self.port))
  114.                 server_socket.sendall(b'SENDMESSAGE')
  115.                 sleep(1)
  116.                 server_socket.sendall(str.encode(username))
  117.                 sleep(1)
  118.                 server_socket.sendall(str.encode(to_user))
  119.                 sleep(1)
  120.                 server_socket.sendall(str.encode(message))
  121.                 while True:
  122.                     response = server_socket.recv(1024)
  123.                     if response:
  124.                         result = response.decode()
  125.                         break
  126.                 print(result)
  127.                 self.main(username)
  128.             except:
  129.                 print('Connection error, make sure the server is up and you are connected to the internet!')
  130.                 self.main(username)
  131.  
  132.         elif user_choice == '2':
  133.             dm_list = []
  134.             get_chat_query = 'GETCHATLIST'
  135.             server_socket.connect((self.host, self.port))
  136.             server_socket.sendall(str.encode(get_chat_query))
  137.             sleep(1)
  138.             server_socket.sendall(str.encode(username))
  139.             while True:
  140.                 server_response = server_socket.recv(1024)
  141.                 if server_response:
  142.                     if server_response.decode() == 'FINISH':
  143.                         # print('Quitting loop...')
  144.                         break
  145.                     else:
  146.                         dm_list.append(server_response.decode())
  147.             for chat_log in dm_list:
  148.                 chat_log = chat_log.split(' ')
  149.                 print(chat_log[0])
  150.             # chat_log_to_check = input('Enter the chat log you want to check!')
  151.             chat_log = input('Enter chat log name')
  152.             sleep(3)
  153.             self.get_chat_history(username, chat_log)
  154.         elif user_choice == '3':
  155.             print('Logging out...')
  156.             sleep(1)
  157.             self.account_options()
  158.         else:
  159.             print('Invalid input!')
  160.             sef.main(username)
  161.                
  162. c = Client()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement