Last active
August 29, 2015 14:22
-
-
Save pirafrank/5f857ab3bd0af5e970a3 to your computer and use it in GitHub Desktop.
Local filesharing command line program written in Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Copyright 2014 Francesco Pira <dev@fpira.com> | |
# This program is free software: you can redistribute it and/or modify | |
# it under the terms of the GNU General Public License as published by | |
# the Free Software Foundation, either version 3 of the License, or | |
# (at your option) any later version. | |
# This program is distributed in the hope that it will be useful, | |
# but WITHOUT ANY WARRANTY; without even the implied warranty of | |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
# GNU General Public License for more details. | |
# You should have received a copy of the GNU General Public License | |
# along with this program. If not, see <http://www.gnu.org/licenses/ | |
# 'Freud' LAN File Sharing Program | |
# Version 0.7 | |
# CHANGELOG | |
# v0.7 | |
# added multi-language support | |
# v 0.6.2 | |
# folder creation code is now a function | |
# patched windows bug | |
# patched possible folder creation bug | |
# v 0.6 | |
# added IP format checking | |
# now handling 'no connection' error | |
# added extension presence checking | |
# better functions organization | |
# v 0.5.2 | |
# improved file path string checking | |
# v 0.5.1 | |
# added file path string checking | |
# imports | |
from socket import * | |
import os | |
import sys | |
from lang import * | |
##### variabili globali ########################################################### | |
porta = 9900 | |
nostraDir="File Ricevuti" | |
cartellasalvataggio = os.path.join(os.path.expanduser('~'),nostraDir) | |
##### utilities ################################################################### | |
def filename_check(filename): | |
# controllo path / nome file | |
filename = filename.replace("\\","") | |
if filename[len(filename)-1]==' ': | |
filename = filename[:-1] | |
return filename | |
def has_extension(filename): | |
# return | |
extension = os.path.splitext(filename) | |
if extension[1]=='': | |
return False | |
else: | |
return True | |
def connection_test(): | |
# controllo connessione e ottengo IP | |
try: | |
IP = ([(STest.connect(('8.8.8.8', 80)), STest.getsockname()[0], STest.close()) for STest in [socket(AF_INET, SOCK_DGRAM)]][0][1]) | |
return IP | |
except: | |
print 'Errore di connessione. Controlla la tua connessione alla rete locale e le impostazioni del tuo firewall.' | |
sys.exit() | |
def is_ipv4(ip): | |
#True = IP ok | |
#False = IP non conforme | |
if ip[len(ip)-1]=='.': return False | |
parts = ip.split(".") | |
if len(parts) != 4: | |
return False | |
for item in parts: | |
if not 0 < int(item) < 255: | |
return False | |
return True | |
def make_folder(): | |
# creazione e controllo cartella salvataggio in locale | |
try: | |
os.makedirs(cartellasalvataggio, 0777) | |
except OSError: | |
if not os.path.isdir(cartellasalvataggio): | |
print 'Impossibile creare cartella destinazione!' | |
sys.exit() | |
##### funzione server (riceve) ##################################################### | |
def server(IP): | |
make_folder() | |
# socket | |
serverSock = socket(AF_INET, SOCK_STREAM) | |
serverSock.bind(('', porta)) | |
serverSock.listen(1) | |
print "Il tuo IP e'",IP | |
# ottengo nome file per il salvataggio in locale | |
filename = raw_input('Dai un nome al file da salvare\nNote: Gli omonimi saranno sovrascritti. Inserisci la stessa estensione del file da inviare: ') | |
# controllo estensione file | |
while 1: | |
if has_extension(filename) == False: | |
filename = raw_input('Sembra tu non abbia inserito alcuna estensione, riprova: ') | |
else : | |
break | |
print "Il file sara' salvato in 'File Ricevuti' nella tua cartella utente" | |
print "Pronto a ricevere il file. Invia dall'altro PC" | |
# ricezione file | |
connSock, indirizzo = serverSock.accept() | |
file = open(os.path.join(cartellasalvataggio,filename), 'wb') | |
while 1: | |
data = connSock.recv(1024) | |
if not data: break | |
file.write(data) | |
# finalizzazione | |
print 'Ricevuto!' | |
file.close() | |
connSock.close() | |
##### funzione client (invia) ###################################################### | |
def client(): | |
# input nome file e controllo | |
while 1: | |
filename = raw_input("Inserisci il nome del file da inviare: ") | |
try: | |
if os.name == 'posix': | |
file = open(filename_check(filename), 'rb') | |
else: | |
file = open(filename,'rb') | |
break | |
except: | |
print "File non esistente, riprova!" | |
# input IP ricevente | |
serverip = raw_input("inserisci l'IP del destinatario: ") | |
while 1: | |
ipformat = is_ipv4(serverip) | |
if ipformat == False: | |
serverip = raw_input('IP non conforme, riprova: ') | |
else: | |
break | |
# socket | |
clientSock = socket(AF_INET, SOCK_STREAM) | |
clientSock.connect((serverip, porta)) | |
# lettura stream byte e invio | |
while 1: | |
data = file.read(1024) | |
if not data: break | |
clientSock.sendall(data) | |
# finalizzazione | |
print 'Inviato!' | |
file.close() | |
clientSock.close() | |
##### MAIN ######################################################################## | |
def main(): | |
scelta = 0 | |
print " ::::::: Programma di file sharing :::::::" | |
IP = connection_test() | |
while 1: | |
print " ::::::: MENU :::::::" | |
print " 1) INVIA UN FILE" | |
print " 2) RICEVI UN FILE" | |
print " 3) ESCI" | |
print " Nota: i due computer devono appartenere alla stessa rete locale" | |
scelta = raw_input(" inserisci la tua scelta: ") | |
if scelta == "1": | |
client() | |
elif scelta == "2": | |
server(IP) | |
elif scelta == "3": | |
sys.exit() | |
break | |
else: | |
print "Spiacente, scelta non consentita!" | |
##### Programma ################################################################### | |
if __name__ == "__main__": | |
main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment