-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.py
58 lines (47 loc) · 1.51 KB
/
client.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
import socket
import random as rand
from dns_resolver import DNSresolver as Resolver
CMD_END = 'end'
def read_input(hint = '>'):
'''Reads the input unitl it's a valid input.
'''
while True:
text = input(hint)
if len(text) > 0:
return text
class Client():
'''The client provides the user interface. It is responsible for
reading the user questions, sending it to the resolver and presenting
the answer to the user.
'''
def __init__(self):
self.ip = 'localhost'
self.port = rand.randint(50000, 52999)
def start(self):
self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.socket.bind((self.ip, self.port))
print('\n===================================================')
print('Seja bem vindo!')
print(f'Para buscar um domínio, digite o nome do domínio')
print(f'Para sair, digite {CMD_END}')
print('===================================================\n')
def resolve(self, question: str):
resolver = Resolver(question, self.socket)
return resolver.getHostIP()
def close(self):
print('Cliente encerrado! Até logo!')
self.socket.close()
def main():
client = Client()
client.start()
while True:
cmd = read_input()
if cmd == CMD_END:
client.close()
break
else:
result = client.resolve(cmd)
print(result)
print()
if __name__ == '__main__':
main()