Advertisement
DrAungWinHtut

fileclient6.py

Feb 3rd, 2024
918
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.92 KB | None | 0 0
  1. import socket
  2. import os
  3.  
  4. def receive_data(sock):
  5.     try:
  6.         data = sock.recv(1024).decode('utf-8')
  7.         return data
  8.     except UnicodeDecodeError:
  9.         # Handle binary data
  10.         return sock.recv(1024)
  11.  
  12. def download_file(server_socket, filename):
  13.     with open(filename, 'wb') as f:
  14.         while True:
  15.             data = server_socket.recv(1024)
  16.             if not data:
  17.                 break
  18.             f.write(data)
  19.  
  20. def main():
  21.     host = "127.0.0.1"
  22.     port = 5001
  23.  
  24.     s = socket.socket()
  25.     s.connect((host, port))
  26.  
  27.     while True:
  28.         # 1. Receive the file list from the server
  29.         menu = receive_data(s)
  30.         print("Menu:")
  31.         print(menu)
  32.         ans = input('Choose (1,2,3)...')
  33.         s.send(ans.encode())
  34.         if ans == '4':
  35.             print('Exiting....')
  36.             s.close()
  37.             exit(0)
  38.    
  39.        
  40.  
  41. if __name__ == "__main__":
  42.     main()
  43.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement