-
Notifications
You must be signed in to change notification settings - Fork 1
/
dns.py
67 lines (48 loc) · 1.53 KB
/
dns.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
"""
DNS application
"""
from twisted.internet.error import DNSLookupError
from twisted.names.client import getHostByName
from twisted.names.error import DNSNameError
from twisted.web import http
from twisted.web.iweb import IRequest
from ._main import main
from .klein import Klein, KleinRenderable
__all__ = (
"Application",
)
class Application(object):
"""
DNS application.
Application that performs DNS queries.
"""
router = Klein()
main = classmethod(main) # type: ignore
@router.route("/")
def root(self, request: IRequest) -> KleinRenderable:
"""
Application root resource.
Responds with a message noting the nature of the application.
:param request: The request to respond to.
"""
return "DNS API."
@router.route("/gethostbyname/<name>")
async def hostname(self, request: IRequest, name: str) -> KleinRenderable:
"""
Hostname lookup resource.
Performs a lookup on the given name and responds with the resulting
address.
:param request: The request to respond to.
:param name: A host name.
"""
try:
address = await getHostByName(name)
except DNSNameError:
request.setResponseCode(http.NOT_FOUND)
return "no such host"
except DNSLookupError:
request.setResponseCode(http.NOT_FOUND)
return "lookup error"
return address
if __name__ == "__main__": # pragma: no cover
Application.main() # type: ignore