-
Notifications
You must be signed in to change notification settings - Fork 0
/
dns_server.py
47 lines (34 loc) · 1.22 KB
/
dns_server.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
import socket
import threading
from dns_resolver import DNSResolver
class DnsServerTcp:
def __init__(self):
self.port = 53
self.server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.server_socket.bind(("", self.port))
self.server_socket.listen()
def run(self):
while True:
conn, address = self.server_socket.accept()
user_request = conn.recv(1024)
resp, ans = DNSResolver().resolve(user_request)
conn.send(ans)
class DnsServerUdp:
def __init__(self):
self.port = 53
self.server_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.server_socket.bind(("", self.port))
def run(self):
while True:
user_request, address = self.server_socket.recvfrom(1024)
resp, ans = DNSResolver().resolve(user_request)
self.server_socket.sendto(ans, address)
def main():
tcp_server = DnsServerTcp()
udp_server = DnsServerUdp()
tcp = threading.Thread(target=tcp_server.run)
udp = threading.Thread(target=udp_server.run)
tcp.start()
udp.start()
if __name__ == '__main__':
main()