Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from datetime import datetime
- import socket
- LISTEN_HOST = "localhost"
- LISTEN_PORT = 420
- STOP_KEY = "8"
- BUFFER_SIZE = 1024
- def main():
- KEYS = {"1": albums_list(), "2": songs_list_in_album(), "3": song_length(), "4": song_lyrics(),
- "5": find_album_by_song(), "6": find_song_by_name(), "7": find_song_by_lyrics(), "8": "Session closed."}
- listening_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- listening_sock.bind((LISTEN_HOST, LISTEN_PORT))
- listening_sock.listen(1) # Begin listening
- while True:
- client_msg = ""
- print('[' + datetime.now().strftime("%H:%M:%S") + "] [LISTENING] "
- + LISTEN_HOST + ':' + str(LISTEN_PORT) + '\n')
- client_sock, client_address = listening_sock.accept() # Connected to client
- print('[' + datetime.now().strftime("%H:%M:%S") + "] [CONNECTED] "
- + client_address[0] + ':' + str(client_address[1]) + '\n')
- client_sock.sendall("Welcome to the PinkFloyd Server!".encode()) # Send welcome message
- while client_msg != STOP_KEY:
- try:
- client_msg = client_sock.recv(BUFFER_SIZE).decode() # Get message
- except Exception as e:
- print("Error: ", e)
- client_sock.close()
- break
- if client_msg in KEYS:
- info = KEYS[client_msg] # Get the request info
- else:
- info = "Wrong selection =/"
- try:
- client_sock.sendall(info.encode()) # Send the requested info
- except Exception as e:
- print("Error: ", e)
- client_sock.close()
- break
- # Stop key pressed
- print('[' + datetime.now().strftime("%H:%M:%S") + "] [CLOSED] "
- + client_address[0] + ':' + str(client_address[1]) + '\n')
- client_sock.close()
- def albums_list():
- return "- Albums list -"
- def songs_list_in_album():
- return "- List of songs in the album -"
- def song_length():
- return "- Song length - "
- def song_lyrics():
- return "- Song lyrics - "
- def find_album_by_song():
- return "- Find album by the song name - "
- def find_song_by_name():
- return "- Find song by the name - "
- def find_song_by_lyrics():
- return "- Find song by his lyrics - "
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement