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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
Binary file added client/__pycache__/split_lib.cpython-312.pyc
Binary file not shown.
2 changes: 1 addition & 1 deletion client/examples/https_client.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import requests

# The URL of the Flask server
url = 'http://localhost:5000/'
url = 'http://localhost:5001/'
url_upload = url + 'upload'
url_download = url + 'download/'

Expand Down
104 changes: 101 additions & 3 deletions client/solution.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,34 @@

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)

CHUNK_SIZE = 1024
HTTPS_SERVER_URL = "http://127.0.0.1:5001"
TCP_SERVER_IP = "'127.0.0.1'"
TCP_SERVER_PORT = 65432

def split_store(file):
with open(file, 'rb') as f:
chunk_num = 0
while True:
chunk = f.read(CHUNK_SIZE)
if not chunk:
break

if chunk_num % 2 == 0:
chunk = f"HTTPS{chunk_num}".encode() + chunk + f"{chunk_num}SPTTH".encode()
upload_helper1_https(chunk, f"{file}_chunk_{chunk_num}")
else:
chunk = f"TCP{chunk_num}".encode() + chunk + f"{chunk_num}PCT".encode()
upload_helper2_tcp(chunk, f"{file}_chunk_{chunk_num}")

chunk_num += 1

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

#split_fetch(test_file)
def split_fetch(file):
chunk_num = 0
with open(file, 'wb') as f:
while True:
try:
if chunk_num % 2 == 0:
chunk = fetch_https_helper3(f"{file}_chunk_{chunk_num}")
else:
chunk = fetch_tcp_helper4(f"{file}_chunk_{chunk_num}")

if not chunk:
break
# cleaned_chunk = clean_chunk(chunk, chunk_num)
# f.write(cleaned_chunk)
chunk_num += 1
except Exception as e:
print(f"Error fetching chunk {chunk_num}: {e}")
break

def upload_helper1_https(chunk, chunk_filename):
url = f"{HTTPS_SERVER_URL}/upload"
files = {'file': (chunk_filename, chunk)}

try:
response = requests.post(url, files=files)
response.raise_for_status()
print(f"Chunk {chunk_filename} uploaded to HTTPS server.")
except requests.exceptions.RequestException as e:
print(f"Failed to upload chunk {chunk_filename} to HTTPS server: {e}")

def upload_helper2_tcp(chunk, chunk_filename):
try:
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((TCP_SERVER_IP, TCP_SERVER_PORT))
s.sendall("upload".encode().ljust(1024))
s.sendall(chunk_filename.encode().ljust(1024))
s.sendall(str(len(chunk)).encode().ljust(1024))
s.sendall(chunk)
print(f"Chunk {chunk_filename} uploaded to TCP server.")
except Exception as e:
print(f"Failed to upload chunk {chunk_filename} to TCP server: {e}")

def fetch_https_helper3(chunk_filename):
url = f"{HTTPS_SERVER_URL}/download/{chunk_filename}"
try:
response = requests.get(url)
response.raise_for_status()
print(f"Chunk {chunk_filename} fetched from HTTPS server.")
return response.content
except requests.exceptions.RequestException as err:
print(f"Failed to fetch chunk {chunk_filename} from HTTPS server: {err}")
return None

def fetch_tcp_helper4(chunk_filename):
try:
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((TCP_SERVER_IP, TCP_SERVER_PORT))
s.sendall("download".encode().ljust(1024))
s.sendall(chunk_filename.encode().ljust(1024))

file_size = int(s.recv(1024).decode().strip())
chunk_data = s.recv(file_size)
print(f"Chunk {chunk_filename} fetched from TCP server.")
return chunk_data
except Exception as err:
print(f"Failed to fetch chunk {chunk_filename} from TCP server: {err}")
return None

def clean_chunk(chunk, chunk_num):
if chunk_num % 2 == 0:
header = f"HTTPS{chunk_num}".encode()
footer = f"{chunk_num}SPTTH".encode()
else:
header = f"TCP{chunk_num}".encode()
footer = f"{chunk_num}PCT".encode()

return chunk[len(header):-len(footer)]




Expand Down
110 changes: 103 additions & 7 deletions client/split_lib.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,105 @@
# Library file for both the functions

def split_store(file_name) :
# Split store code
pass
import os
import requests
import socket

def split_fetch(file_name) :
# Split fetch code'
pass

CHUNK_SIZE = 1024
HTTPS_SERVER_URL = "http://127.0.0.1:5001"
TCP_SERVER_IP = "'127.0.0.1'"
TCP_SERVER_PORT = 65432

def split_store(file):
with open(file, 'rb') as f:
chunk_num = 0
while True:
chunk = f.read(CHUNK_SIZE)
if not chunk:
break

if chunk_num % 2 == 0:
chunk = f"HTTPS{chunk_num}".encode() + chunk + f"{chunk_num}SPTTH".encode()
upload_helper1_https(chunk, f"{file}_chunk_{chunk_num}")
else:
chunk = f"TCP{chunk_num}".encode() + chunk + f"{chunk_num}PCT".encode()
upload_helper2_tcp(chunk, f"{file}_chunk_{chunk_num}")

chunk_num += 1

def split_fetch(file):
chunk_num = 0
with open(file, 'wb') as f:
while True:
try:
if chunk_num % 2 == 0:
chunk = fetch_https_helper3(f"{file}_chunk_{chunk_num}")
else:
chunk = fetch_tcp_helper4(f"{file}_chunk_{chunk_num}")

if not chunk:
break
cleaned_chunk = clean_chunk(chunk, chunk_num)
f.write(cleaned_chunk)
chunk_num += 1
except Exception as e:
print(f"Error fetching chunk {chunk_num}: {e}")
break

def upload_helper1_https(chunk, chunk_filename):
url = f"{HTTPS_SERVER_URL}/upload"
files = {'file': (chunk_filename, chunk)}

try:
response = requests.post(url, files=files)
response.raise_for_status()
print(f"Chunk {chunk_filename} uploaded to HTTPS server.")
except requests.exceptions.RequestException as e:
print(f"Failed to upload chunk {chunk_filename} to HTTPS server: {e}")

def upload_helper2_tcp(chunk, chunk_filename):
try:
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((TCP_SERVER_IP, TCP_SERVER_PORT))
s.sendall("upload".encode().ljust(1024))
s.sendall(chunk_filename.encode().ljust(1024))
s.sendall(str(len(chunk)).encode().ljust(1024))
s.sendall(chunk)
print(f"Chunk {chunk_filename} uploaded to TCP server.")
except Exception as e:
print(f"Failed to upload chunk {chunk_filename} to TCP server: {e}")

def fetch_https_helper3(chunk_filename):
url = f"{HTTPS_SERVER_URL}/download/{chunk_filename}"
try:
response = requests.get(url)
response.raise_for_status()
print(f"Chunk {chunk_filename} fetched from HTTPS server.")
return response.content
except requests.exceptions.RequestException as err:
print(f"Failed to fetch chunk {chunk_filename} from HTTPS server: {err}")
return None

def fetch_tcp_helper4(chunk_filename):
try:
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((TCP_SERVER_IP, TCP_SERVER_PORT))
s.sendall("download".encode().ljust(1024))
s.sendall(chunk_filename.encode().ljust(1024))

file_size = int(s.recv(1024).decode().strip())
chunk_data = s.recv(file_size)
print(f"Chunk {chunk_filename} fetched from TCP server.")
return chunk_data
except Exception as err:
print(f"Failed to fetch chunk {chunk_filename} from TCP server: {err}")
return None

def clean_chunk(chunk, chunk_num):
if chunk_num % 2 == 0:
header = f"HTTPS{chunk_num}".encode()
footer = f"{chunk_num}SPTTH".encode()
else:
header = f"TCP{chunk_num}".encode()
footer = f"{chunk_num}PCT".encode()

return chunk[len(header):-len(footer)]
File renamed without changes.
2 changes: 1 addition & 1 deletion https_server/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,4 @@ def download_file(filename):


if __name__ == "__main__":
app.run(host='0.0.0.0',debug=True, port=5000)
app.run(host='0.0.0.0',debug=True, port=5001)
Loading