read_query.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #!/usr/bin/python3
  2. # Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
  3. #
  4. # Permission to use, copy, modify, and/or distribute this software for any
  5. # purpose with or without fee is hereby granted, provided that the above
  6. # copyright notice and this permission notice appear in all copies.
  7. #
  8. # THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  9. # REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  10. # AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  11. # INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  12. # LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  13. # OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  14. # PERFORMANCE OF THIS SOFTWARE.
  15. import re
  16. import sys
  17. def read_query(file, querylist):
  18. fh = open(file)
  19. while True:
  20. query = {}
  21. query['header'] = {}
  22. line = fh.readline()
  23. if not line: break
  24. line = line.rstrip('\n')
  25. if re.search('^#', line): continue
  26. if re.search('^\s*$', line): continue
  27. fields = line.split(' ')
  28. query['header']['id'] = fields.pop(0)
  29. query['header']['qr'] = fields.pop(0)
  30. query['header']['opcode'] = int(fields.pop(0))
  31. query['header']['aa'] = fields.pop(0)
  32. query['header']['tc'] = fields.pop(0)
  33. query['header']['rd'] = fields.pop(0)
  34. query['header']['ra'] = fields.pop(0)
  35. query['header']['z'] = fields.pop(0)
  36. query['header']['ad'] = fields.pop(0)
  37. query['header']['cd'] = fields.pop(0)
  38. query['header']['rcode'] = fields.pop(0)
  39. query['header']['qdcount'] = fields.pop(0)
  40. query['header']['ancount'] = fields.pop(0)
  41. query['header']['nscount'] = fields.pop(0)
  42. query['header']['arcount'] = fields.pop(0)
  43. if query['header']['opcode'] == 0:
  44. get_qtuple(query, 'question', fields)
  45. querylist.append(query)
  46. fh.close()
  47. def get_qtuple(query, sectname, list):
  48. if sectname == 'question':
  49. count = int(query['header']['qdcount'])
  50. item = {}
  51. i = 0
  52. while i < count:
  53. query[sectname] = []
  54. item['qname'] = list.pop(0)
  55. item['qtype'] = list.pop(0)
  56. item['qclass'] = list.pop(0)
  57. query[sectname].append(item)
  58. i += 1
  59. def print_query(querylist):
  60. keylist = ['id', 'qr', 'opcode', 'aa', 'tc', 'rd', 'ra', 'z',
  61. 'ad', 'cd', 'rcode', 'qdcount', 'ancount', 'nscount',
  62. 'arcount']
  63. for q in querylist:
  64. for key in keylist:
  65. print(q['header'][key], ' ')
  66. print_question(q)
  67. def print_question(query):
  68. i = 0
  69. while i < len(query['question']):
  70. print(query['question'][i]['qname'], \
  71. query['question'][i]['qtype'], \
  72. query['question'][i]['qclass'], \
  73. sep=' ')
  74. i += 1
  75. if __name__ == '__main__':
  76. qlist = []
  77. read_query(sys.argv[1], qlist)
  78. print_query(qlist)