forked from fortisauris/PyDevJR_Course
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsession11.py
298 lines (220 loc) · 6.65 KB
/
session11.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
import http.client
import json
'''
Jednoduchy http client na stahovanie html kodu pomocou metody GET z internetu.
'''
con = http.client.HTTPSConnection("fortisauris.com") # HTTP a HTTPS
con.request("GET", "/")
r1 = con.getresponse() # urob volaco
headers = r1.getheaders()
print(r1.status, r1.reason)
print("HEADERS :\n", headers)
obsah = r1.read(500)
print(obsah)
'''
Jednoduchy http klient, ktory pomocou PUT vklada na server udaje
'''
BODY = '*** CONTENT ***'
con = http.client.HTTPConnection('localhost', 8000)
con.request('PUT', '/file', BODY)
response = con.getresponse()
print(response.status, response.reason)
# TODO Metoda DELETE - Nastudovat
# TODO Metoda PATCH - Nastudovat
'''
Jednoduchy http klient, ktory pripravuje request POST a posiela udaje na server alebo do formulara
'''
# { # typicke vyuzitie metody POST
# 'login':'uzivatel45',
# 'password':'bryndz123'
# }
con = http.client.HTTPSConnection('www.httpbin.org') # server na testovanie http requestov
headers = {'Content-type': 'application/json'}
foo = {'text': "TOTO JE NAS TEXT"}
json_data = json.dumps(foo)
con.request('POST', '/post', json_data, headers)
res = con.getresponse()
# print(res.read().decode())
'''
Jednoduchy http klient pomocou kniznice urllib
'''
import urllib.request
with urllib.request.urlopen("https://fortisauris.com") as f:
obsah = f.read().decode('utf8')
print(obsah)
'''
# R O Z C V I C K A - H L A D A N I E V H T M L K O D E
# pomocou htttp.clienta alebo urllib otvorit stranku
# pomocou metody GET ziskat html
# vybrat z html kodu vsetky linky
# najdete emailove adresy alebo linky
# 'href='
# 'src='
# '@'
# LINKY
# EMAILY
'''
'''
jednoduchy klient, ktory nacitava obsah stranky pomocou GET
'''
con = http.client.HTTPSConnection("fortisauris.com") # HTTP a HTTPS
con.request("GET", "/")
r1 = con.getresponse() # urob volaco
headers = r1.getheaders()
print(r1.status, r1.reason)
print("HEADERS :\n", headers)
obsah = bytes(r1.read()).decode('utf8')
print(obsah)
'''
D E K O R A T O R Y A F U N K C I E V Y S S I E H O R A D U
@staticmethod - Basic Class - staticka metoda bez pouzitia premennych triedy pristupna aj bez iniciacie
@classmethod - Basic Class - Metoda triedy, ktora nepouziva premenne triedy a moze byt vyuzita bez jeje iniciacie
@property - Advanced Class - Metoda triedy, ktora oznaci metodu ako majetok a zacne sa spravat ako premenna triedy
'''
def hello(func):
def wrapper(): # toto je vnutorna funkcia
print("UROB NIECO PRED VLOZENOU FUNKCIOU")
func()
print("UROB NIECO PO SKONCENI VLOZENEJ FUNKCIE")
return wrapper
@hello
def nasa_funkcia():
print("UROB VOLACO")
# hello(nasa_funkcia)
nasa_funkcia() # volame nasu funkciu
'''
F U N K C I A D E K O R A T O R A S P A R A M E T R A M I (*args, **kwargs)
'''
def hello(func):
def wrapper(*args, **kwargs): # vsetky argumenty a kvaziargumenty
print("UROB NIECO PRED VLOZENOU FUNKCIOU")
func(*args, **kwargs)
print("UROB NIECO PO SKONCENI VLOZENEJ FUNKCIE")
return wrapper
@hello
def pozdrav(meno: str, pocet: int):
for i in range(pocet):
print("AHOJ", meno)
pozdrav("Jozko", 3) # volame nasu funkciu
'''
Dekorator @salba sa nas snazi presvedcit, ze 2 a 2 nie su 4 !
'''
def salba(func):
def wrapper(*args, **kwargs):
result = func(*args, **kwargs)
return result + 1
return wrapper
@salba
def spocitaj(a,b):
return a + b
print(spocitaj(2, 2))
'''
Priklad vyuzitia dvoch dekoratorov sucasne
'''
def decor1(func):
def wrapper():
x = func()
return x * x
return wrapper
def decor2(func):
def wrapper():
x = func()
return x * 2
return wrapper
@decor1
@decor2
def num():
return 10
print(num()) # volame nasu dvojdekorovanu funkciu
'''
PRIKLAD VYUZITIA DEKORATORA PRI VYKONANI ULOZENIA NASTAVENI VYZADUJE ABY UZIVATEL BOL ADMIN
'''
USER = "admin"
def admin(func):
def wrapper(*args, **kwargs):
if USER == "admin":
func(*args, **kwargs)
print("NASTAVENIA ULOZENE")
return True # flag
else:
print("UZIVATEL ",USER ," NEMA OPRAVNENIE UKLADAT ZMENY")
return False
return wrapper
@admin
def uloz_nastavenia_serveru():
print("CONFIG ULOZENY")
uloz_nastavenia_serveru() # volame ulozenie nastaveni servera
# a = admin(uloz_nastavenia_serveru)
# print(a())
'''
P A L I N D R O M A L G O R I T M U S- Slovo, ktore je rovnake odzadu aj odpredu
'''
s = 'bryndza'
def palindrome(s):
for i in range(len(s)):
t = s[:i] + s[i+1:]
if t == t[::-1]:
return True
return False
# print(palindrome(s))
# E N I G M A P L U G B O A R D
def plugboard(mods: dict, key):
abeceda = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
defaultne_nastavenie = dict(zip(abeceda, abeceda))
# print(defaultne_nastavenie)
defaultne_nastavenie.update(mods)
# defaultne nastavenie sa zmeni na modifikovane nastavenie
# print(defaultne_nastavenie)
return defaultne_nastavenie[key]
# NASTAVENIE MODIFIKACII NA PLUGBOARDE
mods = {"A":"Z",
"Z": "A",
"Y": "C",
"C": "K",
"K": "Y"
}
'''
msg = "BRYNDZAZDRAZELA"
enc_msg = ""
for i in msg:
enc_znak = plugboard(mods=mods, key=i)
enc_msg += enc_znak
print(msg, "\t", enc_msg)
'''
# TODO Prestavka do 20:05
'''
E N I G M A - pridame si dalsi rotor
'''
abeceda = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
sprava = "BRYNDZAZDRAZELABRYNDZAZDRAZELA"
i = abeceda.index("R") # pociatocne nastavenie valca
nastaveny_valec_1 = abeceda[i:] + abeceda[:i]
print(nastaveny_valec_1)
i = abeceda.index("S") # pociatocne nastavenie valca
nastaveny_valec_2 = abeceda[i:] + abeceda[:i]
print(nastaveny_valec_2)
i = abeceda.index("F") # pociatocne nastavenie valca
nastaveny_valec_3 = abeceda[i:] + abeceda[:i]
print(nastaveny_valec_3)
def posun_valca(nastaveny_valec):
valec = nastaveny_valec[1:] + nastaveny_valec[0]
print(valec)
return valec
zasifrovana_sprava = ""
posun_valcov = 0
for i in range(0, len(sprava)):
enc_znak = nastaveny_valec_1[abeceda.index(sprava[i])] #
enc_znak = nastaveny_valec_2[abeceda.index(enc_znak)]
enc_znak = nastaveny_valec_3[abeceda.index(enc_znak)]
# TODO Reverzny valec
enc_znak = plugboard(mods=mods, key=enc_znak) # Plugboard
nastaveny_valec_1 = posun_valca(nastaveny_valec_1)
if posun_valcov == 26:
nastaveny_valec_2 = posun_valca(nastaveny_valec_2) # TODO Ak Valec 2 dosiahne 26 ???
posun_valcov = 0
zasifrovana_sprava += enc_znak
posun_valcov += 1
print(sprava)
print(zasifrovana_sprava)
# vysledok bez plugboardu: PGOEVSUUZOYYEMCEVDTKHJJODNNUCS
# vysledok s plugboardom: PGOEVSUUAOCCEMKEVDTYHJJODNNUKS