Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import socket
- import sys
- import time
- from threading import Thread
- from time import sleep
- class bcolors:
- HEADER = '\033[95m'
- OKBLUE = '\033[94m'
- OKGREEN = '\033[92m'
- WARNING = '\033[93m'
- FAIL = '\033[91m'
- ENDC = '\033[0m'
- BOLD = '\033[1m'
- UNDERLINE = '\033[4m'
- def showHelp():
- print bcolors.FAIL+"\tAvailable commands\n"+bcolors.ENDC
- print "/chat <username>"+"\t\t"+"to chat with other users"
- print "/help"+"\t\t\t\t"+"to show available commands"
- print "/logout"+"\t\t\t\t"+"to logout"
- print "/whosol"+"\t\t\t\ts"+"show current online users"
- print "\n"
- def recieve(client_socket):
- global sock_status
- while True:
- if sock_status is False:
- break
- data=client_socket.recv(1024)
- print "\t\t\t\t"+data
- def send(client_socket,username,target):
- global method
- global status
- global init
- global sock_status
- init=False
- while method=="Connect":
- message=raw_input()
- if message=="/logout":
- client_socket.send(target+":"+message+":"+username)
- method="Disconnect"
- sleep(0.8)
- print "Logging out..."
- status=False
- init=False
- sock_status=False
- break
- else:
- message=target+":"+message+":"+username
- client_socket.send(message)
- sock_status=True
- method=True
- status=False
- init=False
- def main():
- global method
- global status
- global init
- global sock_status
- print bcolors.WARNING+"ChitChat initiate..."
- sleep(2)
- acc=[]
- login_status=True
- while login_status is True:
- while status==False:
- print bcolors.WARNING+"Prompt Login Attempt..."
- sleep(0.8)
- st=True
- while st==True:
- sleep(0.8)
- print bcolors.WARNING+"Type login <username> to login or register <username> to register"+bcolors.ENDC
- login=raw_input("")
- try:
- command,username=login.split(" ")
- if command=="login":
- acc=[]
- password=raw_input("Password : ")
- st=False
- db=open('database','r+')
- for line in db:
- line=line.replace("\n","")
- acc.append(line)
- db.close()
- elif command=="register":
- acc=[]
- password=raw_input("Register password for this account : ")
- temp_acc=username+" "+password
- print temp_acc
- db=open('database','r+')
- for line in db:
- line=line.replace("\n","")
- acc.append(line)
- db.write(temp_acc+"\n")
- db.close()
- sleep(0.8)
- print "Account Registered. Please login..."
- elif command=="exit":
- quit()
- for i in range(0,len(acc)):
- temp_username,temp_password=acc[i].split(" ")
- if temp_username==username and temp_password==password:
- status=True
- init=True
- sock_status=True
- method="Connect"
- break
- if status==False:
- sleep(0.8)
- print bcolors.FAIL+"Username or password is incorrect. Re-attempt login...\n"
- except:
- if login=="exit":
- quit()
- else:
- print bcolors.FAIL+"to login, please specify your username"
- if init is True:
- # Inisialisasi Create a TCP/IP socket
- client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- #Koneksi
- server_address = ('localhost', 10002)
- print >>sys.stderr, 'connecting to %s port %s' % server_address
- client_socket.connect(server_address)
- client_socket.sendto(username,server_address)
- if init is True:
- while True:
- sleep(0.8)
- print "type /help to show available commands"
- com=raw_input()
- try:
- comm,target=com.split(" ")
- if comm=="/chat":
- sleep(0.2)
- print "Entering chat mode..."
- sleep(0.5)
- print "Starting chat with %s\n"%(target)
- sleep(0.2)
- print bcolors.OKBLUE+target+bcolors.ENDC
- break
- except:
- if com=="/help":
- showHelp()
- elif com=="/logout":
- client_socket.close()
- if init is True:
- Thread(target=send,args=(client_socket,username,target,)).start()
- Thread(target=recieve,args=(client_socket,)).start()
- if sock_status is False:
- client_socket.close()
- if __name__=="__main__":
- main()
Add Comment
Please, Sign In to add comment