Advertisement
Guest User

PORCODDIOOOO

a guest
Mar 25th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1. from socket import *
  2. import os
  3.  
  4. serverPort = 6666
  5. path = '/'
  6.  
  7.  
  8. #Socket che accetta tutte le connessioni in entrata
  9. welcomeSocket = socket(AF_INET, SOCK_STREAM)
  10. welcomeSocket.bind(('', serverPort))
  11. welcomeSocket.listen(10)
  12. print('Server started on port', serverPort)
  13. while True:
  14. connectionSocket, addr = welcomeSocket.accept()
  15. #Appena accetta la connessione la mantiene aperta
  16. while True:
  17. #readline() + rstrip() per rimuovere \n + split() per creare la lista di parole
  18. client_command = connectionSocket.makefile().readline().rstrip().split()
  19. command = client_command[0].lower()
  20. #mantengo tutti i comandi in minuscolo
  21.  
  22. #ROUTINE
  23. if command == 'content':
  24. print("DIR")
  25. buffer = ''
  26. for item in os.listdir(path):
  27. buffer += '> ' + item + '\n'
  28. connectionSocket.makefile("w").writelines(buffer)
  29.  
  30. elif command == 'mv':
  31. if os.path.isdir(client_command[1]):
  32. path = client_command[1]
  33. connectionSocket.makefile('w').writelines('DIR> ' + path + '\n')
  34. else:
  35. connectionSocket.makefile('w').writelines('Directory non esistente.\n')
  36.  
  37. elif command == 'get':
  38. if os.path.isfile(path + '/' + client_command[1]):
  39. with open(path + '/' + client_command[1], 'r') as file_buffer:
  40. content = file_buffer.read()
  41. connectionSocket.makefile('w').writelines(content + '<EOF>' + '\n')
  42. else:
  43. connectionSocket.makefile('w').writelines('File non esistente.\n')
  44. else:
  45. connectionSocket.makefile('w').writelines('Comando non riconosciuto.\n')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement