Advertisement
Yonka2019

client.py

May 2nd, 2021
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.36 KB | None | 0 0
  1. from datetime import datetime
  2. import socket
  3.  
  4. SERVER_IP = "localhost"
  5. SERVER_PORT = 420
  6.  
  7. STOP_KEY = "8"
  8. BUFFER_SIZE = 1024
  9.  
  10.  
  11. def main():
  12.     sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  13.  
  14.     sock.connect((SERVER_IP, SERVER_PORT))  # Connects to the server
  15.     client_input = ""
  16.     print('[' + datetime.now().strftime("%H:%M:%S") + "] [CONNECTED] " + SERVER_IP + ':' + str(SERVER_PORT) + '\n')
  17.  
  18.     print(sock.recv(BUFFER_SIZE).decode())  # Print welcome message
  19.  
  20.     while client_input != STOP_KEY:
  21.         show_menu()  # Shows the menu
  22.         client_input = input("> ")  # Get request
  23.  
  24.         try:
  25.             sock.sendall(client_input.encode())  # Send request for the info
  26.  
  27.             print(sock.recv(BUFFER_SIZE).decode())  # Get the requested info
  28.         except Exception as e:
  29.             print("Error: ", e)
  30.             sock.close()
  31.             break
  32.  
  33.     # Stop key pressed
  34.     sock.close()
  35.  
  36.  
  37. def show_menu():
  38.     """
  39.    Shows the menu
  40.    """
  41.     print("""
  42. - [MENU] Select by the digit [MENU] -
  43. 1. Get list of the albums
  44. 2. Get list of the songs in the album
  45. 3. Get length of song
  46. 4. Get LYRICS of song
  47. 5. Find in which album is the song
  48. 6. Search song by the name
  49. 7. Search song by the LYRICS
  50. 8. Quit (Close connection)
  51. - [MENU] Select by the digit [MENU] -
  52.    """)
  53.  
  54.  
  55. if __name__ == '__main__':
  56.     main()
  57.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement