Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from socket import *
- import os
- serverPort = 6666
- path = '/'
- #Socket che accetta tutte le connessioni in entrata
- welcomeSocket = socket(AF_INET, SOCK_STREAM)
- welcomeSocket.bind(('', serverPort))
- welcomeSocket.listen(10)
- print('Server started on port', serverPort)
- while True:
- connectionSocket, addr = welcomeSocket.accept()
- #Appena accetta la connessione la mantiene aperta
- while True:
- #readline() + rstrip() per rimuovere \n + split() per creare la lista di parole
- client_command = connectionSocket.makefile().readline().rstrip().split()
- command = client_command[0].lower()
- #mantengo tutti i comandi in minuscolo
- #ROUTINE
- if command == 'content':
- print("DIR")
- buffer = ''
- for item in os.listdir(path):
- buffer += '> ' + item + '\n'
- connectionSocket.makefile("w").writelines(buffer)
- elif command == 'mv':
- if os.path.isdir(client_command[1]):
- path = client_command[1]
- connectionSocket.makefile('w').writelines('DIR> ' + path + '\n')
- else:
- connectionSocket.makefile('w').writelines('Directory non esistente.\n')
- elif command == 'get':
- if os.path.isfile(path + '/' + client_command[1]):
- with open(path + '/' + client_command[1], 'r') as file_buffer:
- content = file_buffer.read()
- connectionSocket.makefile('w').writelines(content + '<EOF>' + '\n')
- else:
- connectionSocket.makefile('w').writelines('File non esistente.\n')
- else:
- connectionSocket.makefile('w').writelines('Comando non riconosciuto.\n')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement