Newer
Older
# 1. feladat
## 1.a interaktív mód
A Python nyelv gyakran használt tudományos és matematika algoritmusok implementálására. Írassuk ki mérföldben 3 tizedes jegy pontossággal egy félmaraton hosszát az interpreter segítségével!
Megoldás:
```bash
$ python3
>>> dist = 21097.5
>>> dist
21097.5
>>> mile = 1.6093
>>> mile
1.6093
>>> dist_in_miles = (dist / 1000) / mile
>>> dist_in_miles
13.109737152799354
>>> print("Distance of a half maraton in miles is: {:.3f}".format(dist_in_miles))
Distance of a half maraton in miles is: 13.110
```
Ahogy látható, itt elmentem változókba a kapott konstansokat, majd megformázva irattam ki a kapott értéket.
## 1.b Feladat - Külső modul használata
**Feladat:** Írassuk ki a *google.com* (nem kell a *www* elé!) tartománynévhez rendelt levelező szervereket (MX rekord)!
Megoldás:
```bash
$ python3
>>> import dns.resolver
>>> answers = dns.resolver.query('google.com', 'MX')
<stdin>:1: DeprecationWarning: please use dns.resolver.resolve() instead
>>> for rdata in answers:
... print('Host', rdata.exchange, 'has preference', rdata.preference
...
Host aspmx.l.google.com. has preference 10
Host alt3.aspmx.l.google.com. has preference 40
Host alt4.aspmx.l.google.com. has preference 50
Host alt1.aspmx.l.google.com. has preference 20
Host alt2.aspmx.l.google.com. has preference 30
```
Miután beimportáltam a megfelelő könyvtárat, lekérdeztem a `google.com` `MX` rekordjait. A depricated warningot
figyelmen kívül hagyva egy `for` ciklussal kiíratom a kapott adatokat.
## 1.c Feladat - Saját DNS modul
**Feladat:** Hosszabb kódok esetén az interaktív interpreter használata körülményes. Írjunk Python modult `dns_tool.py`
néven az előző feladatban megvalósított funkcióhoz!
from dns.resolver import query
from dns.exception import DNSException
# input: dns name and optional the type of the record and return all the answered data in a list or in
# case of exception an empty list
def get_mx(dns_name, type="MX"):
try:
answers = query(dns_name, type)
except DNSException:
return []
ret = []
for a in answers:
element = (str(a.preference), str(a.exchange))
ret.append(element)
return ret
```
Futtatás:
```bash
$ python3
>>> import dns_tool
>>> help()
help> dns_tool.get_mx
Help on function get_mx in dns_tool:
dns_tool.get_mx = get_mx(dns_name, type='MX')
# input: dns name and optional the type of the record and return all the answered data in a list
# or in case of exception an empty list
>>> list = dns_tool.get_mx('google.com')
>>> for e in list:
... print(e)
...
('40', 'alt3.aspmx.l.google.com.')
('20', 'alt1.aspmx.l.google.com.')
('50', 'alt4.aspmx.l.google.com.')
('10', 'aspmx.l.google.com.')
('30', 'alt2.aspmx.l.google.com.')
>>>
>>> list = dns_tool.get_mx('nemletezik.asd')
>>> for e in list:
... print(e)
...
>>>
Készítettem egy függvényt két paraméterrel, majd a korábbi feladat lépéseit implementáltam, csak most függvénybe zárva.
Tesztelésnél látszik, hogy a `google.com`-ra szépen jönnek a válaszok, míg a `nemletezik.asd`-re nem, és hibát se dob a
kód, egyszerűen üres listát ad vissza.
## 1.d Feladat - Futtatható DNS szkript
**Feladat:** Tegyük futtathatóvá az elkészült modult!
A forráskód:
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
#!/usr/bin/env python3
from dns.resolver import query
from dns.exception import DNSException
# input: dns name and optional the type of the record and return all the answered data in a list or in
# case of exception an empty list
def get_mx(dns_name, type="MX"):
try:
answers = query(dns_name, type)
except DNSException:
return []
ret = []
for a in answers:
element = (str(a.preference), str(a.exchange))
ret.append(element)
return ret
if __name__ == "__main__":
inp = input("Add meg a lekérdezendő domain-t, valamint ha szeretnéd specifikálni, a rekord típusát is szóközzel "
"elválasztva (az alapértelmezett rekord típus az MX): ")
inp = inp.split(" ")
if len(inp) >= 2:
list = get_mx(inp[0], inp[1])
for e in list:
print(e)
else:
list = get_mx(inp[0])
for e in list:
print(e)
```
Ahhoz, hogy futtatthatóvá tegyem, elkészítettem neki a `__name__` "függvényt". Itt beállítottam, hogy kérjen be adatot a
felhasználótól, és kb leimplementáltam, ahogy eddig kézzel teszteltem.
Futtatás:
```bash
$ chmod +x dns_tool2.py
$ ./dns_tool2.py
Add meg a lekérdezendő domain-t, valamint ha szeretnéd specifikálni, a rekord típusát is szóközzel
elválasztva (az alapértelmezett rekord típus az MX): google.com MX ./dns_tool2.py:11: DeprecationWarning:
please use dns.resolver.resolve() instead
answers = query(dns_name, type)
('10', 'aspmx.l.google.com.')
('20', 'alt1.aspmx.l.google.com.')
('50', 'alt4.aspmx.l.google.com.')
('40', 'alt3.aspmx.l.google.com.')
('30', 'alt2.aspmx.l.google.com.')
```