Advertisement
DrAungWinHtut

fileserver6.py

Feb 3rd, 2024
735
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.58 KB | None | 0 0
  1. import socket
  2. import os
  3. import threading  # Import the threading module
  4.  
  5. def downloadFile(client_socket,addr):
  6.     pass
  7.  
  8. def uploadFile(client_socket,addr):
  9.     pass
  10.  
  11. def chat(client_socket,addr):
  12.     pass
  13.  
  14. def handle_client(client_socket,addr):
  15.    
  16.     menu = '1-Download File\n2-Upload File\n3-Chat\n4-Exit\n'
  17.     # 2. Send the file list to the client
  18.     client_socket.send(menu.encode())
  19.  
  20.     # 3. Receive the selected file name from the client
  21.     choice = client_socket.recv(1024).decode()
  22.     match(choice):
  23.         case '1':
  24.             print('Download File')
  25.             downloadFile(client_socket,addr)
  26.  
  27.         case '2':
  28.             print('Upload File')
  29.             uploadFile(client_socket,addr)
  30.         case '3':
  31.             print('Chat')
  32.             chat(client_socket,addr)
  33.         case '4':
  34.             print(f'client {addr} is disconnecting')
  35.             client_socket.close()
  36.             exit(0)
  37.     handle_client(client_socket,addr)
  38.    
  39.    
  40.  
  41. def list_files_in_current_dir():
  42.     current_dir = os.getcwd()
  43.     files = [f for f in os.listdir(current_dir) if os.path.isfile(os.path.join(current_dir, f))]
  44.     return files
  45.  
  46. host = "127.0.0.1"
  47. port = 5001
  48. server = socket.socket()
  49. server.bind((host, port))
  50. server.listen()
  51.  
  52. print('Server is Listening.....')
  53.  
  54. while True:  # Keep accepting connections
  55.     conn, addr = server.accept()
  56.     print(f'Accepted connection from {addr}')
  57.    
  58.     # Create a new thread to handle the client
  59.     client_handler = threading.Thread(target=handle_client, args=(conn,addr))
  60.     client_handler.start()
  61.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement