Advertisement
lecterthemolecter

client

May 19th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.72 KB | None | 0 0
  1. import socket
  2.  
  3. def menu():
  4.     print("\nEnter a choice from the menu:\n1.list albums\n2.list songs in album\n3.find length of song\n4.get lyrics of song\n5.find album which song is in\n6.search for songs by title\n7.search for songs by lyrics\n8.exit\n")
  5.  
  6. def main():
  7.     SERVER_IP = "127.0.0.1"
  8.     count = 0
  9.     SERVER_PORT = 25565
  10.     server_address = (SERVER_IP, SERVER_PORT)
  11.     while(True):
  12.         try:
  13.             sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  14.             sock.connect(server_address)
  15.         except Exception as exep:
  16.             print("Error: %s" % exep)
  17.             break
  18.         if count == 0:
  19.             sock.sendall("HELLO#".encode())
  20.             server_msg = sock.recv(1024).decode()
  21.             print(server_msg)
  22.             count += 1
  23.             continue
  24.         if count == 1:
  25.             sock.sendall("LOGIN#".encode())
  26.             server_msg = sock.recv(1024).decode()
  27.             print(server_msg)
  28.             pwd = input()
  29.             try:
  30.                 sock.sendall(pwd.encode())
  31.                 server_msg = sock.recv(1024).decode()
  32.             except Exception as e:
  33.                 print("Error: %s" % e)
  34.                 break
  35.             if server_msg == 'Wrong password!':
  36.                 print(server_msg)
  37.                 break
  38.             print(server_msg)
  39.             count+=1
  40.             continue
  41.  
  42.         menu()
  43.         user_choice = input("Enter a choice: ")
  44.  
  45.         if user_choice == '1':
  46.             try:
  47.                 sock.sendall("GETALBUMS#".encode())
  48.             except Exception as e:
  49.                 print("Error: %s" % e)
  50.                 break
  51.         elif user_choice == '2':
  52.             song = input("Enter an album: ")
  53.             try:
  54.                 sock.sendall(("GETSONGS#" + song).encode())
  55.             except Exception as e:
  56.                 print("Error: %s" % e)
  57.                 break
  58.         elif user_choice == '3':
  59.             song = input("Enter a song: ")
  60.             try:
  61.                 sock.sendall(("GETLENGTH#" + song).encode())
  62.             except Exception as e:
  63.                 print("Error: %s" % e)
  64.                 break
  65.         elif user_choice == '4':
  66.             song = input("Enter a song: ")
  67.             try:
  68.                 sock.sendall(("GETLYRICS#" + song).encode())
  69.             except Exception as e:
  70.                 print("Error: %s" % e)
  71.                 break
  72.         elif user_choice == '5':
  73.             song = input("Enter a song: ")
  74.             try:
  75.                 sock.sendall(("SONGALBUM#" + song).encode())
  76.             except Exception as e:
  77.                 print("Error: %s" % e)
  78.                 break
  79.         elif user_choice == '6':
  80.             search_term = input("Enter a search term: ")
  81.             try:
  82.                 sock.sendall(("SEARCHSONG#" + search_term).encode())
  83.             except Exception as e:
  84.                 print("Error: %s" % e)
  85.                 break
  86.         elif user_choice == '7':
  87.             search_term = input("Enter a search term: ")
  88.             try:
  89.                 sock.sendall(("SEARCHLYRIC#" + search_term).encode())
  90.             except Exception as e:
  91.                 print("Error: %s" % e)
  92.                 break
  93.         elif user_choice == '8':
  94.             try:
  95.                 sock.sendall("EXIT#".encode())
  96.             except Exception as e:
  97.                 print("Error: %s" % e)
  98.                 break
  99.             server_msg = sock.recv(1024).decode()
  100.             print(server_msg)
  101.             break
  102.         else:
  103.             print("Wrong input! try again.\n")
  104.             continue
  105.  
  106.         server_msg = sock.recv(1024).decode()
  107.         print(server_msg)
  108.         input("\nPress Enter to continue...")
  109.  
  110.  
  111. if __name__ == '__main__':
  112.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement