-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfileget.py
executable file
·153 lines (125 loc) · 4.39 KB
/
fileget.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/usr/bin/env python3
#IPK projekt 1
#Adriana Jurkechová, xjurke02@fit.stud.vutbr.cz
#2020/2021
import sys, getopt, os, re, socket
def arguments(argv):
nameserver = ''
surl = ''
try:
opts, args = getopt.getopt(argv,"hn:f:",["servername=","url="])
except getopt.GetoptError:
print ("fileget.py -n NAMESERVER -f SURL")
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print ("fileget.py -n NAMESERVER -f SURL")
sys.exit()
elif opt in ("-n", "--servername"):
nameserver = arg
elif opt in ("-f", "--url"):
surl = arg
return nameserver, surl
def NSP(domenove_jmeno, ipcka, port):
MESSAGE = "WHEREIS " + domenove_jmeno
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
while True:
sock.sendto(MESSAGE.encode('utf-8'), (ipcka, int(port)))
data, address = sock.recvfrom(4096)
if data.decode('utf-8') == "ERR Not Found":
print("Súborový server nebol nájdený\n", file=sys.stderr)
sys.exit(2)
elif data.decode('utf-8') == "ERR Syntax":
print("Chybná syntax požiadavky alebo mena suborového serveru\n", file=sys.stderr)
sys.exit(2)
elif "OK" in data.decode('utf-8'):
break
sock.close()
response = data.decode('utf-8').split(':')
if not response[1].isnumeric():
print("Chybná odpoved menneho serveru\n", file=sys.stderr)
sys.exit(2)
return response[1]
def FSP(domenove_jmeno, nazev_suboru, ipcka, port):
MESSAGE = "GET " + nazev_suboru + " FSP/1.0\r\nHostname: " + domenove_jmeno + "\r\nAgent: xjurke02\r\n\r\n"
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(30)
s.connect((ipcka, int(port)))
s.send(MESSAGE.encode('utf-8'))
data = s.recv(100000)
if data.decode('utf-8') == "FSP/1.0 Bad Request":
print("Chybná syntax požiadavky.\n", file=sys.stderr)
sys.exit(2)
elif data.decode('utf-8') == "FSP/1.0 Not Found":
print("Nepodarilo sa nájsť", nazev_suboru, ".\n", file=sys.stderr)
elif data.decode('utf-8') == "FSP/1.0 Server Error":
print("Niečo sa stalo so serverom.\n", file=sys.stderr)
sys.exit(2)
f= open(nazev_suboru,"wb+")
data = s.recv(100000)
f.write(data)
while True:
data = s.recv(100000)
f.write(data)
if not data: break
s.close()
f.close()
def GET(splitted_surl, ipcka, new_port):
#GET ALL
if "*" == splitted_surl[3]:
FSP(splitted_surl[2], "index", ipcka, new_port)
f=open("index", "r")
if f.mode == 'r':
#citanie indexu riadok po riadku
while line := f.readlines():
for x in range(len(line)):
line[x]=line[x].rstrip("\n")
path = line[x]
#pripad s viacerymi podadresarmi
if "/" in line [x]:
dirs = []
parts = line[x].split('/')
for x in range(0, len(parts)-1):
dirs.append(parts[x])
dirs = '/'.join(dirs)
if not os.path.isdir(dirs):
os.makedirs(dirs)
FSP(splitted_surl[2], path, ipcka, new_port)
else:
FSP(splitted_surl[2], line[x], ipcka, new_port)
#GET
else:
FSP(splitted_surl[2], splitted_surl[3], ipcka, new_port)
nameserver, surl = arguments(sys.argv[1:])
#oddelenie ip a portu
splitted_nameserver = nameserver.split(':')
ipcka = splitted_nameserver[0]
port = splitted_nameserver[1]
#oddelenie surl podla /
splitted_surl = surl.split('/')
#osetrenie nazvu servera
if splitted_surl[0] != "fsp:":
print("Prosím zadaj fsp protokol.\n", file=sys.stderr)
sys.exit(2)
#kontrola nazvu menneho suboru
if not bool(re.match('[\w._-]+$', splitted_surl[2], re.IGNORECASE)):
print("Menny server sa sklada z nepovolenych znakov.\n", file=sys.stderr)
sys.exit(2)
#spojenie nazvu
if len(splitted_surl) > 4:
path = []
dirs = []
#vytvorenie adresarovej struktury
for x in range(3, len(splitted_surl)-1):
dirs.append(splitted_surl[x])
dirs = '/'.join(dirs)
if not os.path.isdir(dirs):
os.makedirs(dirs)
#spojenie predtym rozdeleneho nazvu do jedneho stringu
for x in range(3, len(splitted_surl)):
path.append(splitted_surl[x])
path = '/'.join(path)
splitted_surl[3] = path
new_port = NSP(splitted_surl[2], ipcka, port)
GET(splitted_surl, ipcka, new_port)
sys.exit(0)