-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclient.py
123 lines (101 loc) · 3.9 KB
/
client.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
from Util import header, broker, subscribe, socketsRepo
import json
import argparse
import zmq
import random
import json
import os
import sys
import hashlib
from dotenv import load_dotenv
import socket as so
load_dotenv()
MAX_RANGE = int(os.getenv('MAX_RANGE'))
SUCCESS_CODE = os.getenv('SUCCESS_CODE')
SUCCESS_CODE_ALREADY_HAVE_FILE = os.getenv('SUCCESS_CODE_ALREADY_HAVE_FILE')
context = zmq.Context()
def getMyIP():
s = so.socket(so.AF_INET, so.SOCK_DGRAM)
s.connect(("8.8.8.8", 80))
IPAddr = s.getsockname()[0]
return IPAddr
def upload(fileName, address, firstNode, myAddress):
headerJSON = header.uploadFile(fileName, path="")
hashes = broker.sendFile(headerJSON, address, firstNode)
hs = header.sendMagnetLink(fileName, headerJSON["Hash"], headerJSON["Size"], hashes)
magnetLink = json.dumps(hs).encode()
sha256 = hashlib.sha256()
sha256.update(magnetLink)
magnetLinkHash = sha256.hexdigest()
fileID = int(magnetLinkHash, 16)%MAX_RANGE
socketsub, _, _, _, _= subscribe.findPosition(firstNode, myAddress, fileID)
broker.sendChunk(magnetLink, socketsub, headerJSON["Name"], headerJSON["Size"], 0, magnetLinkHash, magnetLink=True)
print(magnetLink)
info = f'\n\n The magnetLink for {headerJSON["Name"]} es: \n\n{magnetLinkHash}\n\n'
print(info)
def download(res, firstNode, myAddress):
# res = json.loads(res)
print(res)
# totalBytes = b""
try:
fileFullName, ext = res["Name"].split('.')
except:
ext = ""
fileFullName = res["Name"]
for parts in res["Parts"]:
fileName, _ = parts
print(parts)
fileID = int(fileName, 16)%MAX_RANGE
socketsub, _, _, _, _= subscribe.findPosition(firstNode, myAddress, fileID)
bytes = broker.getFile(socketsub, fileName)
# totalBytes +=
file1 = open(f"{fileFullName}2.{ext}", "ab") # append mode
file1.write(bytes)
file1.close()
print("Saving succesfully: ")
# res = socketsRepo.saveFile( f'{fileName}2.{ext}', totalBytes, path="")
# print(res)
def getMagnetLink(magnetLink, myAddress, firstNode):
try:
fileID = int(magnetLink, 16)%MAX_RANGE
socketsub, _, _, _, code = subscribe.findPosition(firstNode, myAddress, fileID)
if code != SUCCESS_CODE:
bytes = broker.getFile(socketsub, magnetLink)
bytesJson = json.loads(bytes)
res = socketsRepo.saveFile( f'{bytesJson["Name"]}MagnetLink.txt', bytes, path="")
print(res)
return bytesJson
else:
print("Error geting the magnetLink, it does't exist")
return False
except Exception as e:
print(e)
print("Error geting download data")
def main():
parser = argparse.ArgumentParser(
description='Example with nonoptional arguments',
)
parser.add_argument('-address', action="store", type=str, default="localhost:0000")
parser.add_argument('--upload', action="store_true", default=False)
parser.add_argument('--download', action="store_true", default=False)
parser.add_argument('-fileName', action="store", type=str, default="")
parser.add_argument('-magnetLink', action="store", type=str, default="")
# parser.add_argument('-magnetFile', action="store", type=str, default="")
data = parser.parse_args()
if data.upload and data.fileName == "":
print("--upload flag, must to specify -fileName")
return
if data.download and data.magnetLink == "":
print("--download flag, must to specify -magnetLink")
return
firtsNode = data.address
fileName = data.fileName
magnetLink = data.magnetLink
myAddress = getMyIP()
if data.upload:
upload(fileName, myAddress, firtsNode, myAddress)
if data.download:
res= getMagnetLink(magnetLink, myAddress, firtsNode)
if res:
download(res, firtsNode, myAddress)
main()