├── README.md ├── fileReceiver.py └── fileSender.py /README.md: -------------------------------------------------------------------------------- 1 | File-Transfer-Python 2 | ==================== 3 | 4 | By Blas Rodríguez Irízar. 5 | 6 | A small application for file transfer with Python and Sockets 7 | 8 | I've been doing it to explore the sockets library for Python. I hope it would help someone just for the sake of learning 9 | or even for transfering fiels trough a local network for example (it goes pretty fast). 10 | 11 | There is a Server file to browse the file you want to send. You'll have to bind the server to an IP and choose any port 12 | (maybe you should open it on the configuration of your router). 13 | The client side just receives the file. You have the posibility to name it (remember to add the extension!). 14 | 15 | 16 | -------------------------------------------------------------------------------- /fileReceiver.py: -------------------------------------------------------------------------------- 1 | import os 2 | import socket 3 | PORT = 8080 4 | HOST = 'localhost' 5 | ANSI_BACKGROUND = '\033[44m' 6 | ANSI_RED = '\033[31m' 7 | ANSI_BLUE = '\033[34m' 8 | ESCAPEANSI = '\033[0m' 9 | print ANSI_BLUE + 'Welcome to the Data Transfer application' 10 | nombrearchivo = raw_input('define a name with its extension').strip(' ') 11 | socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 12 | socket.connect((HOST, PORT)) 13 | filename = socket.recv(1024) 14 | #fname = open('./asd.pdf', 'wb') 15 | fname = open('./'+nombrearchivo, 'wb') 16 | 17 | while True: 18 | strng = socket.recv(1024) 19 | if strng: 20 | fname.write(strng) 21 | else: 22 | fname.close() 23 | break 24 | socket.close() 25 | print ESCAPEANSI 26 | print ANSI_BACKGROUND + ANSI_RED + 'Data received correctly' 27 | print ESCAPENSI 28 | exit() 29 | -------------------------------------------------------------------------------- /fileSender.py: -------------------------------------------------------------------------------- 1 | import os 2 | import socket 3 | PORT = 8080 4 | HOST = 'localhost' 5 | socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 6 | socket.bind((HOST,PORT)) 7 | socket.listen(1) 8 | conn, addr = socket.accept() 9 | print '\033[46m\033[34m\033[1mBienvenido al File Sender v.0.02 hecho en Python. Este programa permite enviar archivos a traves de tu maquina\033[0m' 10 | ANSI_BACKGROUND = '\033[46m' 11 | ANSI_RED = '\033[31m' 12 | ANSI_BLUE = '\033[34m' 13 | ESCAPEANSI = '\033[0m' 14 | def seleccion_path(): 15 | PATH = raw_input('\033[34m\033[1mSelect the Path (./ by default)').strip('n') 16 | if PATH == '': 17 | PATH = os.getcwd() 18 | print PATH, ESCAPEANSI 19 | acepta_path = raw_input('\033[34m\033[1mSi o No (S/N)').lower().strip(' ') 20 | if acepta_path == 's' or acepta_path == 'si': 21 | return PATH 22 | else: 23 | seleccion_path() 24 | def filesDir(path): 25 | files = os.listdir(PATH) 26 | for fl in files: 27 | i = int(files.index(fl))+1 28 | print ANSI_RED + str(i)+ ')' + fl 29 | return files 30 | 31 | PATH = seleccion_path() 32 | print 'el PATH seleccionado es:', PATH + '\n' 33 | filesDir(PATH) 34 | fileSelected = int(raw_input(ANSI_BLUE + 'Select a file with the number').strip(' ').lower()) 35 | print PATH + filesDir(PATH)[fileSelected-1] 36 | 37 | filepath = PATH + filesDir(PATH)[fileSelected-1] 38 | #envia nombre del file 39 | conn.send(filepath) 40 | qLines = len(open(PATH + filesDir(PATH)[fileSelected-1], 'rb').readlines()) 41 | fileToSend = open(filepath, 'rb') 42 | while True: 43 | data = fileToSend.readline() 44 | if data: 45 | conn.send(data) 46 | else: 47 | break 48 | fileToSend.close() 49 | conn.sendall('') 50 | conn.close() 51 | print '\033[43m File sent' 52 | #Finaliza el programa y deja los codigos ANSI cerrados 53 | print ESCAPEANSI 54 | exit() 55 | --------------------------------------------------------------------------------