Python: DNS Client implementation

Source: Internet
Author: User

In the past two days, I tested IPv6 and DNS, checked the materials at night, and wrote a DNS Client. For the request message encoding section, refer to the online instance.

The encoding has just been completed and the function has not been tested.

1. Code:

#! /Usr/bin/ENV Python #-*-coding: UTF-8-*-import configparserimport socketimport time import stringimport osimport reimport structdef dns_codec (hostname): ''' function: request message encoding input: Hostname: host name, such as www.baidu.com output: encoded byte stream Author: Socrates Date: 2012-12-14 ''' Index = OS. urandom (2) hoststr = ''. join (CHR (LEN (x) + X for X in hostname. split ('. ') Data =' % s \ x01 \ x00 \ x00 \ x01 \ x00 \ x00 \ x00 \ x00 \ x00 \ x00 % s \ x0 0 \ x00 \ x01 \ x00 \ x01 '% (index, hoststr) Data = struct. Pack ('! H ', Len (data) + data return datadef dns_decode (in_sock): ''' function: Return Message decoding input: in_sock: Socket output for receiving messages: decoded content Author: Socrates Date: 2012-12-14 ''' rfile = in_sock.makefile ('rb') size = struct. unpack ('! H', rfile. read (2) [0] DATA = rfile. read (size) iplist = Re. findall ('\ xc0. \ x00 \ x01 \ x00 \ x01. {6 }(. {4}) ', data) return ['. '. join (STR (ord (x) for X in S) for S in iplist] def dns_sendmsg (): ''' function: Send DNS query message input: None output: none Author: Socrates Date: 2012-12-14 ''' ens_client_config = configparser. configparser () # Read the configuration file try: ens_client_config.readfp (open ('ens _ client_config.ini ') using t configparser. error: Print 'read ens_client_config.ini failed. '# obtain the required information server_ip_1 = ens_client_config.get ("server_info", "ip_1") server_port_1 = forward ("server_info", "port_1") sockettype_1 = ens_client_config.get ("server_info ", "sockettype_1") heartbeat_1 = ens_client_config.get ("server_info", "heartbeat_1") msg_1 = forward ("server_info", "msg_1") # IP type address_family = {true: socket. af_inet6, false: socket. af_inet} [':' In server_ip_1] # transmission type socket_type = {true: socket. sock_stream, false: socket. sock_dgram} ['tcp '= sockettype_1.upper ()] Try: sock = socket. socket (address_family, socket_type) cannot socket. error, E: Print 'create socket return error. errno = % d, errmsg = % s' % (E. ARGs [0], E. ARGs [1]) # connect to the server and send the message try: # connect to the server sock. connect (server_ip_1, String. atoi (server_port_1) while true: # Sending frequency time. sleep (string. atoi (heartbeat_1) # Send the sock message. sendall (dns_codec (msg_1) # receives and prints the message print dns_decode (sock) handle T socket. error, E: Print 'connect server failed. errno = % d, errmsg = % s' % (E. ARGs [0], E. ARGs [1]) sock. close () If _ name _ = '_ main _': dns_sendmsg ()

2. configuration file:

File Name: ens_client_config.ini

Content:

[server_info]ip_1=192.168.1.1port_1=53sockettype_1=UDPheartbeat_1=2msg_1=www.google.com

3. In the morning, the DNS Client under UDP protocol is tested, and the function is available normally.

A. Wireshark packet capture:

Request:

Response:


B. Code running result:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.