Advertisement
Yonka2019

server.py

May 2nd, 2021
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.40 KB | None | 0 0
  1. from datetime import datetime
  2. import socket
  3.  
  4. LISTEN_HOST = "localhost"
  5. LISTEN_PORT = 420
  6.  
  7. STOP_KEY = "8"
  8. BUFFER_SIZE = 1024
  9.  
  10.  
  11. def main():
  12.     KEYS = {"1": albums_list(), "2": songs_list_in_album(), "3": song_length(), "4": song_lyrics(),
  13.             "5": find_album_by_song(), "6": find_song_by_name(), "7": find_song_by_lyrics(), "8": "Session closed."}
  14.  
  15.     listening_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  16.  
  17.     listening_sock.bind((LISTEN_HOST, LISTEN_PORT))
  18.  
  19.     listening_sock.listen(1)  # Begin listening
  20.  
  21.     while True:
  22.         client_msg = ""
  23.         print('[' + datetime.now().strftime("%H:%M:%S") + "] [LISTENING] "
  24.               + LISTEN_HOST + ':' + str(LISTEN_PORT) + '\n')
  25.  
  26.         client_sock, client_address = listening_sock.accept()  # Connected to client
  27.         print('[' + datetime.now().strftime("%H:%M:%S") + "] [CONNECTED] "
  28.               + client_address[0] + ':' + str(client_address[1]) + '\n')
  29.  
  30.         client_sock.sendall("Welcome to the PinkFloyd Server!".encode())  # Send welcome message
  31.  
  32.         while client_msg != STOP_KEY:
  33.             try:
  34.                 client_msg = client_sock.recv(BUFFER_SIZE).decode()  # Get message
  35.             except Exception as e:
  36.                 print("Error: ", e)
  37.                 client_sock.close()
  38.                 break
  39.  
  40.             if client_msg in KEYS:
  41.                 info = KEYS[client_msg]  # Get the request info
  42.             else:
  43.                 info = "Wrong selection =/"
  44.  
  45.             try:
  46.                 client_sock.sendall(info.encode())  # Send the requested info
  47.             except Exception as e:
  48.                 print("Error: ", e)
  49.                 client_sock.close()
  50.                 break
  51.  
  52.         # Stop key pressed
  53.         print('[' + datetime.now().strftime("%H:%M:%S") + "] [CLOSED] "
  54.               + client_address[0] + ':' + str(client_address[1]) + '\n')
  55.         client_sock.close()
  56.  
  57.  
  58. def albums_list():
  59.     return "- Albums list -"
  60.  
  61.  
  62. def songs_list_in_album():
  63.     return "- List of songs in the album -"
  64.  
  65.  
  66. def song_length():
  67.     return "- Song length - "
  68.  
  69.  
  70. def song_lyrics():
  71.     return "- Song lyrics - "
  72.  
  73.  
  74. def find_album_by_song():
  75.     return "- Find album by the song name - "
  76.  
  77.  
  78. def find_song_by_name():
  79.     return "- Find song by the name - "
  80.  
  81.  
  82. def find_song_by_lyrics():
  83.     return "- Find song by his lyrics - "
  84.  
  85.  
  86. if __name__ == '__main__':
  87.     main()
  88.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement