Newer
Older
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
# 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
```
## 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
```
## 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)
...
>>>
```
## 1.d Feladat - Futtatható DNS szkript
**Feladat:** Tegyük futtathatóvá az elkészült modult!
A forráskód:
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
#!/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)
```
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.')
```