#! /usr/bin/env python # -*- coding: UTF-8 -*- """ PyShare: programma per trasferire i file creato da Frafra (francesco.it@gmail.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 2 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. """ # Descrizione del file __module_name__ = "PyShare" __module_version__ = __revision__ = versione = "0.2.3-client" __module_description__ = "Trasferimento file" # Importazione delle librerire import os, socket, stat, sys, time, thread, md5 from itertools import cycle def load(txt): """ Barra di caricamento """ print txt, " ", while 1: for x in cycle("|/-\\"): sys.stdout.write("\b"+x) sys.stdout.flush() time.sleep(0.2) if end: thread.exit_thread() def getline(conn): """ Funzione per ricevere dati """ buf = [] while 1: data = conn.recv(1) if not data: print "Errore" sys.exit(1) if data == "\n": break buf.append(data) return "".join(buf) # Verifica dei dati immessi if len(sys.argv) != 4: print "Usare: %s porta ip file" % (sys.argv[0]) sys.exit(1) porta = sys.argv[1] try: porta = int(porta) except: print "Paramentro \"porta\" non valido." sys.exit(1) hostname = sys.argv[2] filename = sys.argv[3] # Inizializzazione fd = os.open(filename, os.O_RDONLY) filesize = os.fstat(fd)[stat.ST_SIZE] s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((hostname, int(porta))) # Contrattazione end = 0 thread.start_new_thread(load, ("Contrattazione in corso...",)) s.send("%s\n" % filename.split(os.sep)[-1]) s.send("%s\n" % filesize) data = int(s.recv(1)) end = 1 if not data: print "\nConnessione rifiutata." sys.exit(1) # Trasferimento start = time.time() end = 0 thread.start_new_thread(load, ("\nTrasferimento in corso...",)) while 1: buf = os.read(fd, 4096) if not buf: break s.send(buf) os.close(fd) end = 1 # Verifica end = 0 thread.start_new_thread(load, ("\nVerifica intergrità...",)) res = md5.new(open(filename, "rb").read()).hexdigest() s.send("%s\n" % res) data = int(s.recv(1)) if not data: print "\nMd5 errato. Ritrasferire il file." sys.exit(1) end = 1 # Risultato sec = time.time()-start print "\nTrasferimento di %d bytes effettuato." % filesize