def start(self): try: dns_server = socketserver.ForkingUDPServer( (self.listen_ip, self.port), DNSHandler) except Exception as ex: print(ex) dns_server = socketserver.ThreadingUDPServer( (self.listen_ip, self.port), DNSHandler) dns_server.special_host = self.special_hosts dns_server.serve_forever()
def start(self): dns_handler = partial(DNSHandlerV2, self.special_hosts, ("114.114.114.114", 53)) try: dns_server = socketserver.ForkingUDPServer( (self.listen_ip, self.port), dns_handler) except Exception as ex: print(ex) dns_server = socketserver.ThreadingUDPServer( (self.listen_ip, self.port), dns_handler) dns_server.serve_forever()
def start(ServerAddress, applcation): """ Forks application to handle request :param ServerAddress: :param applcation: :return: """ UDPSipServer.application = applcation UDPServerObject = socketserver.ForkingUDPServer( ServerAddress, UDPSipServer) UDPServerObject.serve_forever()
res = "STORED" if res: sent = socket.sendto(res.encode(), self.client_address) if DEBUG: print('sent %s bytes back to %s' % (sent, self.client_address)) print(res) class ThreadedUDPServer(socketserver.ThreadingMixIn, socketserver.UDPServer): pass if __name__ == "__main__": #server = ThreadedUDPServer((server_ip, SERVER_PORT), ThreadedUDPRequestHandler) server = socketserver.ForkingUDPServer((server_ip, SERVER_PORT), ThreadedUDPRequestHandler) server_thread = threading.Thread(target=server.serve_forever) server_thread.daemon = True try: server_thread.start() print("Server started at {} port {}".format(server_ip, SERVER_PORT)) while True: time.sleep(100) except (KeyboardInterrupt, SystemExit): server.shutdown() server.server_close() exit()
def dnsserver(): with socketserver.ForkingUDPServer((settings.DNSHOST, settings.DNSPORT), DNSserver) as s: s.serve_forever()
def dnsresponce(self, request): fun = { 'A': dnsA, 'NS': dnsNS, 'MX': dnsMX, 'CNAME': dnsCNAME, } # 根据查询的类型返回响应的数据 qtype = QTYPE[request.q.qtype] return fun[qtype](request) def savelog(self, request): qname = request.q.qname qtype = QTYPE[request.q.qtype] l = Log(IP=self.client_address[0], qtype=qtype, text=qname) l.save() def dnsserver(): with socketserver.ForkingUDPServer((settings.DNSHOST, settings.DNSPORT), DNSserver) as s: s.serve_forever() if __name__ == '__main__': with socketserver.ForkingUDPServer(('127.0.0.1', 10086), DNSserver) as s: s.serve_forever()
class MyUDPHandler(socketserver.BaseRequestHandler): #服务类,监听绑定等等 def handle(self): #请求处理类,所有请求的交互都是在handle里执行的 # self.request is the UDP socket connected to the client self.data = self.request[0].strip() socket = self.request[1] print("{} sent:".format(self.client_address[0])) print(self.data.decode()) # just send back the same data, but upper-cased socket.sendto(self.data, self.client_address) if __name__ == "__main__": HOST, PORT = socket.gethostbyname(socket.getfqdn( socket.gethostname())), 9686 print(str(HOST) + ':' + str(PORT) + ' is on serve') # Create the server, binding to localhost on port 9999 #server = socketserver.TCPServer((HOST, PORT), MyTCPHandler) server = socketserver.UDPServer((HOST, PORT), MyUDPHandler) # Activate the server; this will keep running until you # interrupt the program with Ctrl-C server.serve_forever() #server = socketserver.ThreadingTCPServer((HOST, PORT), MyTCPHandler) #线程 #server = socketserver.ThreadingUDPServer((HOST, PORT), MyUDPHandler) #线程 #server = socketserver.ForkingTCPServer((HOST, PORT), MyTCPHandler) #多进程TCP linux适用 server = socketserver.ForkingUDPServer((HOST, PORT), MyUDPHandler) #多进程UDP linux适用 # server = socketserver.TCPServer((HOST, PORT), MyTCPHandler) 单进程TCP # server = socketserver.UDPServer((HOST, PORT), MyUDPHandler) 单进程UDP
# Zero length octet record += b'\x00' # TYPE - just copy QTYPE # TODO QTYPE values set is superset of TYPE values set, handle different QTYPEs, see RFC 1035 3.2.3. record += question['qtype'] # CLASS - just copy QCLASS # TODO QCLASS values set is superset of CLASS values set, handle at least * QCLASS, see RFC 1035 3.2.5. record += question['qclass'] # TTL - 32 bit unsigned integer. Set to 0 to inform, that response # should not be cached. record += b'\x00\x00\x00\x00' # RDLENGTH - 16 bit unsigned integer, length of RDATA field. # In case of QTYPE=A and QCLASS=IN, RDLENGTH=4. record += b'\x00\x04' # RDATA - in case of QTYPE=A and QCLASS=IN, it's IPv4 address. # Calculate a random but deterministic answer record += hashlib.md5(b''.join(question['name'])).digest()[:4] records += record return records if __name__ == '__main__': host, port = '', 53 server = socketserver.ForkingUDPServer((host, port), DNSHandler) print('\033[36mStarted DNS server.\033[39m') try: server.serve_forever() except KeyboardInterrupt: server.shutdown() sys.exit(0)