Guest User

Untitled

a guest
Nov 13th, 2017
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.88 KB | None | 0 0
  1. import socket
  2. import sys
  3. import time
  4. from threading import Thread
  5. from time import sleep
  6.  
  7. class bcolors:
  8. HEADER = '\033[95m'
  9. OKBLUE = '\033[94m'
  10. OKGREEN = '\033[92m'
  11. WARNING = '\033[93m'
  12. FAIL = '\033[91m'
  13. ENDC = '\033[0m'
  14. BOLD = '\033[1m'
  15. UNDERLINE = '\033[4m'
  16.  
  17. def showHelp():
  18. print bcolors.FAIL+"\tAvailable commands\n"+bcolors.ENDC
  19. print "/chat <username>"+"\t\t"+"to chat with other users"
  20. print "/help"+"\t\t\t\t"+"to show available commands"
  21. print "/logout"+"\t\t\t\t"+"to logout"
  22. print "/whosol"+"\t\t\t\ts"+"show current online users"
  23. print "\n"
  24.  
  25. def recieve(client_socket):
  26. global sock_status
  27. while True:
  28. if sock_status is False:
  29. break
  30. data=client_socket.recv(1024)
  31. print "\t\t\t\t"+data
  32.  
  33. def send(client_socket,username,target):
  34. global method
  35. global status
  36. global init
  37. global sock_status
  38. init=False
  39. while method=="Connect":
  40. message=raw_input()
  41. if message=="/logout":
  42. client_socket.send(target+":"+message+":"+username)
  43. method="Disconnect"
  44. sleep(0.8)
  45. print "Logging out..."
  46. status=False
  47. init=False
  48. sock_status=False
  49. break
  50. else:
  51. message=target+":"+message+":"+username
  52. client_socket.send(message)
  53. sock_status=True
  54. method=True
  55. status=False
  56. init=False
  57. def main():
  58. global method
  59. global status
  60. global init
  61. global sock_status
  62. print bcolors.WARNING+"ChitChat initiate..."
  63. sleep(2)
  64. acc=[]
  65. login_status=True
  66. while login_status is True:
  67. while status==False:
  68. print bcolors.WARNING+"Prompt Login Attempt..."
  69. sleep(0.8)
  70. st=True
  71. while st==True:
  72. sleep(0.8)
  73. print bcolors.WARNING+"Type login <username> to login or register <username> to register"+bcolors.ENDC
  74. login=raw_input("")
  75. try:
  76. command,username=login.split(" ")
  77. if command=="login":
  78. acc=[]
  79. password=raw_input("Password : ")
  80. st=False
  81. db=open('database','r+')
  82. for line in db:
  83. line=line.replace("\n","")
  84. acc.append(line)
  85. db.close()
  86. elif command=="register":
  87. acc=[]
  88. password=raw_input("Register password for this account : ")
  89. temp_acc=username+" "+password
  90. print temp_acc
  91. db=open('database','r+')
  92. for line in db:
  93. line=line.replace("\n","")
  94. acc.append(line)
  95. db.write(temp_acc+"\n")
  96. db.close()
  97. sleep(0.8)
  98. print "Account Registered. Please login..."
  99. elif command=="exit":
  100. quit()
  101. for i in range(0,len(acc)):
  102. temp_username,temp_password=acc[i].split(" ")
  103. if temp_username==username and temp_password==password:
  104. status=True
  105. init=True
  106. sock_status=True
  107. method="Connect"
  108. break
  109. if status==False:
  110. sleep(0.8)
  111. print bcolors.FAIL+"Username or password is incorrect. Re-attempt login...\n"
  112. except:
  113. if login=="exit":
  114. quit()
  115. else:
  116. print bcolors.FAIL+"to login, please specify your username"
  117.  
  118. if init is True:
  119. # Inisialisasi Create a TCP/IP socket
  120. client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  121. #Koneksi
  122. server_address = ('localhost', 10002)
  123. print >>sys.stderr, 'connecting to %s port %s' % server_address
  124. client_socket.connect(server_address)
  125. client_socket.sendto(username,server_address)
  126.  
  127. if init is True:
  128. while True:
  129. sleep(0.8)
  130. print "type /help to show available commands"
  131. com=raw_input()
  132. try:
  133. comm,target=com.split(" ")
  134. if comm=="/chat":
  135. sleep(0.2)
  136. print "Entering chat mode..."
  137. sleep(0.5)
  138. print "Starting chat with %s\n"%(target)
  139. sleep(0.2)
  140. print bcolors.OKBLUE+target+bcolors.ENDC
  141. break
  142. except:
  143. if com=="/help":
  144. showHelp()
  145. elif com=="/logout":
  146. client_socket.close()
  147. if init is True:
  148. Thread(target=send,args=(client_socket,username,target,)).start()
  149. Thread(target=recieve,args=(client_socket,)).start()
  150.  
  151. if sock_status is False:
  152. client_socket.close()
  153.  
  154. if __name__=="__main__":
  155. main()
Add Comment
Please, Sign In to add comment