title: send DNS queries over UDP to a DNS server using the Python
body: <p>I am going to send DNS queries over UDP to a DNS server using the Python3 programming language. This is a description:</p>
  
  <p>Your program must send requests for either IPv4 ("A) or IPv6 ("AAAA") addresses</p>
  
  <p>Your program must take 3 command-line arguments: </p>
  
  <p>The type of of address requested (denoted with the --type flag), which can have the value 'A' or 'AAAA'</p>
  
  <p>The host name being queried (denoted with the --name flag)</p>
  
  <p>The IP address of the DNS server to query (denoted with the --server flag)</p>
  
  <p>why when I print(raw_bytes2), I received nothing</p>
  
  <pre><code>
  #!/usr/bin/env python3
  
  # Python DNS query client
  #
  # Example usage:
  #   ./dns.py --type=AAAA --name=www.google.com --server=8.8.8.8
  
  # Should provide equivalent results to:
  #   dig www.google.com AAAA @8.8.8.8 +noedns
  #   (note that the +noedns option is used to disable the pseduo-OPT
  #    header that dig adds. Our Python DNS client does not need
  #    to produce that optional, more modern header)
  
  
  import argparse
  import ctypes
  import random
  import socket
  import struct
  import sys
  
  def main():
  
      # Setup configuration
      parser = argparse.ArgumentParser(description='DNS client for ECPE 170')
      parser.add_argument('--type', action='store', dest='qtype',
                          required=True, help='Query Type (A or AAAA)')
      parser.add_argument('--name', action='store', dest='qname',
                          required=True, help='Query Name')
      parser.add_argument('--server', action='store', dest='server_ip',
                          required=True, help='DNS Server IP')
  
      args = parser.parse_args()
      qtype = args.qtype
      qname = args.qname
      server_ip = args.server_ip
      port = 53
      server_address = (server_ip, port)
  
      if qtype not in ("A", "AAAA"):
          print("Error: Query Type must be 'A' (IPv4) or 'AAAA' (IPv6)")
          sys.exit()
  
      # Create UDP socket
      s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
      s.connect((server_ip, port))
  
  
      # Generate DNS request message
  
      # type
      raw_bytes = bytearray()
      if qtype == "A":
          raw_bytes.append(0x00)
          raw_bytes.append(0x01)
      elif qtype == "AAAA":
          raw_bytes.append(0x00)
          raw_bytes.append(0x1c)
      #qname
      split = qname.split(".")
      string = ""
      for i in split:
          hexs = hex(len(i))    
          string += str(hexs)+i
      #string += "0x00"
      raw_bytes +=bytes(string,'ascii')
      #sever 8.8.8.8
      raw_bytes.append(0x08)
      raw_bytes.append(0x08)
      raw_bytes.append(0x08)
      raw_bytes.append(0x08)
  
      # Send request message to server
     # print(raw_bytes)
      bytes_send = s.sendto(raw_bytes,server_address)
  
      # Receive message from server
      max_bytes = 4096
      (raw_bytes2,src_addr) = s.recvfrom(max_bytes)
  
      print(raw_bytes2)
  
  
  
      # Close socket
      # ---------
      s.close()
  
  
  
      # Decode DNS message and display to screen
      print(bytes_send)
     # dns.decode_dns(raw_bytes)
  
  if __name__ == "__main__":
      sys.exit(main())
  </code></pre>