Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import socket
- from time import sleep
- import random
- import threading
- class Client:
- def __init__(self):
- self.host = '192.168.0.250'
- self.port = 9000
- self.account_options()
- def send_account_details(self, username, password):
- server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- try:
- server.connect((self.host, self.port))
- create_account_query = 'CRTACC'
- server.sendall(str.encode(create_account_query))
- sleep(1)
- server.sendall(str.encode(username))
- sleep(1)
- server.sendall(str.encode(password))
- while True:
- server_response = server.recv(1024)
- # print(server_response)
- if server_response:
- result = server_response.decode()
- break
- if result == 'Account has been made!':
- print(result)
- self.account_options()
- except:
- print('Connection error, make sure the server is up and you are connected to the internet!')
- self.account_options()
- def verify_account_details(self, username, password):
- server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- try:
- server_socket.connect((self.host, self.port))
- sign_in_query = 'SIGNIN'
- server_socket.sendall(str.encode(sign_in_query))
- sleep(1)
- server_socket.sendall(str.encode(username))
- sleep(1)
- server_socket.sendall(str.encode(password))
- while True:
- server_response = server_socket.recv(1024)
- if server_response:
- verification_result = server_response.decode()
- break
- print(verification_result)
- if verification_result == 'Password Confirmed!':
- self.main(username)
- elif verification_result == 'Credential Error!':
- self.account_options()
- except:
- print('Connection error, make sure the server is up and you are connected to the internet!')
- self.account_options()
- def account_options(self):
- print('Type in 1 to create a new account! \n')
- print('Type in 2 to sign into an existing account! \n')
- print('Type in 3 to exit program!')
- user_choice = input()
- if user_choice == '1':
- username = input('Username: ')
- if len(username) < 4 or len(username) > 12:
- print('Your username cannot be less than 4 characters and cannot be longer than 12 characters!')
- self.create_account()
- else:
- username = username.replace(' ','')
- print('If you password has spaces they will be removed! \n')
- password = input('Password: ')
- password = password.replace(' ', '')
- self.send_account_details(username, password)
- elif user_choice == '2':
- username = input('Username: ')
- password = input('Password: ')
- self.verify_account_details(username, password)
- elif user_choice == '3':
- sys.exit()
- else:
- print('Invalid command! Restarting...')
- self.account_options()
- def get_chat_history(self, username, chat_log_to_check):
- server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- server.connect((self.host, self.port))
- get_chat_history_query = 'GETCHATHISTORY'
- server.sendall(str.encode(get_chat_history_query))
- sleep(1)
- server.sendall(str.encode(username))
- sleep(1)
- server.sendall(str.encode(chat_log_to_check))
- while True:
- chat_history = server.recv(1024)
- if chat_history:
- print(chat_history.decode())
- break
- self.main(username)
- def main(self, username):
- server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- print('Type in 1 to send a message to another user!')
- print('Type in 2 to check your messages!')
- print('Type in 3 to log out!')
- user_choice = input()
- if user_choice == '1':
- try:
- print('Enter the other person\'s username!')
- to_user = input('Username: ')
- to_user = to_user.strip(' ')
- message = input('Message: ')
- server_socket.connect((self.host, self.port))
- server_socket.sendall(b'SENDMESSAGE')
- sleep(1)
- server_socket.sendall(str.encode(username))
- sleep(1)
- server_socket.sendall(str.encode(to_user))
- sleep(1)
- server_socket.sendall(str.encode(message))
- while True:
- response = server_socket.recv(1024)
- if response:
- result = response.decode()
- break
- print(result)
- self.main(username)
- except:
- print('Connection error, make sure the server is up and you are connected to the internet!')
- self.main(username)
- elif user_choice == '2':
- dm_list = []
- get_chat_query = 'GETCHATLIST'
- server_socket.connect((self.host, self.port))
- server_socket.sendall(str.encode(get_chat_query))
- sleep(1)
- server_socket.sendall(str.encode(username))
- while True:
- server_response = server_socket.recv(1024)
- if server_response:
- if server_response.decode() == 'FINISH':
- # print('Quitting loop...')
- break
- else:
- dm_list.append(server_response.decode())
- for chat_log in dm_list:
- chat_log = chat_log.split(' ')
- print(chat_log[0])
- # chat_log_to_check = input('Enter the chat log you want to check!')
- chat_log = input('Enter chat log name')
- sleep(3)
- self.get_chat_history(username, chat_log)
- elif user_choice == '3':
- print('Logging out...')
- sleep(1)
- self.account_options()
- else:
- print('Invalid input!')
- sef.main(username)
- c = Client()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement