Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions client/solution.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@

from Networks_Hackathon.client.split_lib import *
from split_lib import *
import os
import filecmp

test_file = "test.txt"
tmp_folder = "tmp"
addr = "./client"
debug = False
#split_store(test_file)
split_store(test_file)

if not os.path.exists(os.path.join(addr,tmp_folder)):
os.makedirs(os.path.join(addr,tmp_folder))
Expand All @@ -21,7 +21,7 @@
if(debug):
print(os.path.join(addr,"mem",test_file))

#split_fetch(test_file)
split_fetch(test_file)



Expand Down
70 changes: 70 additions & 0 deletions client/split_lib.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,79 @@
# Library file for both the functions
import requests
import os
import socket
from time import *

HOST = '127.0.0.1' # Server IP
PORT = 65432 # Server port

def split_store(file_name) :
# Split store code
file = open('./client/mem/'+file_name, 'r')
i=0
while 1:
chunk = file.read(1024)
if chunk=='':
break
file_path='./client/mem/'+str(i)+file_name
file_to_send=open(file_path,'w')
file_to_send.write(chunk)
file_to_send.close()
if i%2==0:
with open(file_path, 'rb') as file2:
response = requests.post('http://localhost:5000/upload', files={'file': file2})
else:
file_size = os.path.getsize(file_path)
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as client_socket:
client_socket.connect((HOST, PORT))
client_socket.sendall(b'upload'.ljust(1024))

client_socket.sendall((str(i)+file_name).encode().ljust(1024))
client_socket.sendall(str(file_size).encode().ljust(1024))

with open(file_path, 'rb') as file_to_send:
client_socket.sendall(file_to_send.read())
i+=1
file.close()
pass

def split_fetch(file_name) :
# Split fetch code'
i=0
file_path='./client/mem/'+file_name
file_to_write=open(file_path,'w')
file_to_write.close()
file_to_write=open(file_path,'ab')
cont=True
while cont:
if i%2==0:
response = requests.get('http://localhost:5000/download/'+str(i)+file_name, stream=True)
if response.status_code == 200:
for chunk in response.iter_content(chunk_size=1024):
if chunk:
file_to_write.write(chunk)
else:
cont=False
break
else:
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as client_socket:
client_socket.connect((HOST, PORT))
client_socket.sendall(b'download'.ljust(1024)) # Send operation type

# Send file name
client_socket.sendall((str(i)+file_name).encode().ljust(1024))

# file not present
if client_socket.recv(1024).decode().strip() == 'File not found':
cont=False
client_socket.close()
break

# Receive the entire file in one go

file_data = client_socket.recv(1024)
# Save the file
file_to_write.write(file_data)
i+=1
file_to_write.close()
pass