Last active
April 8, 2021 20:12
-
-
Save fernandoc1/45abe46129d7b664318e to your computer and use it in GitHub Desktop.
Python Socket File Sender/Receiver
This file contains hidden or 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
#! /usr/bin/python | |
import json | |
import socket | |
import md5 | |
import sys | |
import os | |
OUTPUT_PATH = '/mnt/disk1/files/shared_folder/Fernando/new_images/' | |
eggs = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
eggs.bind(('', 8080)) | |
eggs.listen(1) | |
while True: | |
channel, details = eggs.accept() | |
print 'Connection received' | |
contentHeader = channel.recv(2048) | |
if (len(contentHeader) > 0): | |
print contentHeader | |
try: | |
header = json.JSONDecoder().decode(contentHeader) | |
print header | |
channel.send("HEADER_RECEIVED") | |
folderPath = OUTPUT_PATH+'/'+header['folder'] | |
try: | |
print 'Creating path:', folderPath | |
os.makedirs(folderPath) | |
except Exception as e: | |
print e | |
pass | |
expectedContentSize = int(header['length']) | |
outputFile = folderPath+'/'+header['name'] | |
if not os.path.isfile(outputFile): | |
print 'Waiting for content...' | |
fileContent = '' | |
while True: | |
if (len(fileContent) == expectedContentSize): | |
print "File complete" | |
break | |
sys.stdout.write('%s/%s\r' % (len(fileContent), expectedContentSize)) | |
sys.stdout.flush() | |
data = channel.recv(4096) | |
if not data: | |
break | |
fileContent += data | |
m = md5.new() | |
m.update(fileContent) | |
print "MD5 Received:", m.hexdigest() | |
#print fileContent | |
f = open(outputFile, 'wb') | |
f.write(fileContent) | |
f.close() | |
else: | |
data = channel.recv(expectedContentSize) | |
print 'File is already present' | |
channel.send("CONTENT_RECEIVED") | |
except Exception as e: | |
print e | |
channel.close() |
This file contains hidden or 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
#! /usr/bin/python | |
import socket | |
import json | |
import os | |
import sys | |
import md5 | |
ACK_MESSAGE="HEADER_RECEIVED" | |
CONTENT_ACK_MESSAGE="CONTENT_RECEIVED" | |
folderPath = sys.argv[1] | |
for dirname, dirnames, filenames in os.walk(folderPath): | |
for filename in filenames: | |
filePath = (dirname + '/' + filename).replace("\\", "/") | |
print "FOUND FILE: ", filePath | |
length = os.stat(filePath).st_size | |
f = open(filePath, 'rb') | |
fileContent = f.read() | |
f.close() | |
m = md5.new() | |
m.update(fileContent) | |
header = {} | |
header['name'] = filename | |
header['length'] = str(length) | |
header['folder'] = dirname.replace("\\", "/").replace(folderPath, '') | |
header['md5'] = m.hexdigest() | |
print header | |
done = False | |
while not done: | |
try: | |
eggs = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
eggs.connect(('localhost', 8080)) | |
eggs.sendall(json.JSONEncoder().encode(header)) | |
response = eggs.recv(len(ACK_MESSAGE)) | |
print response | |
eggs.sendall(fileContent) | |
response = eggs.recv(len(CONTENT_ACK_MESSAGE)) | |
print response | |
eggs.close() | |
done = True | |
except: | |
print 'Retrying...' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment