from codec import DNSCodec
from codec import DNSRequestType
from codec import DNSResponseType

class DNSPacketFactory:
	request_factory = DNSRequestType
	response_factory = DNSResponseType

	def __init__(self, definitions):
		self.codec = DNSCodec(definitions)

	def serializeRequest(self, request, extended=False):
		encoded = self.codec.encodeRequest(request)
		if extended:
			encoded = struct.pack('>Hs', len(encoded), encoded)

		return encoded

	def normalizeRequest(self, request_s, extended=False):
		if extended:
			length, request_s = struct.unpack('>Hs', request_s)
			if length != len(request_s):
				request_s = ''

		return self.codec.decodeRequest(request_s)

	def createRequestString(self, identifier, request_type, request_name, extended=False):
		request = self.request_factory(identifier)
		request.addQuestion(request_type, request_name)

		encoded = self.codec.encodeRequest(request)
		if extended:
			encoded = struct.pack('>Hs', len(encoded), encoded)

		return encoded

	def serializeResponse(self, response, extended=False):
		encoded = self.codec.encodeResponse(response)
		if extended:
			encoded = struct.pack('>Hs', len(encoded), encoded)

		return encoded

	def normalizeResponse(self, response_s, extended=False):
		if extended:
			length, request_s = struct.unpack('>Hs', response_s)
			if length != len(response_s):
				request_s = ''

		return self.codec.decodeResponse(response_s)

from codec import DNSCodec
from codec import DNSRequestType
from codec import DNSResponseType

import struct

class DNSPacketFactory:
	request_factory = DNSRequestType
	response_factory = DNSResponseType

	def __init__(self, definitions):
		self.codec = DNSCodec(definitions)

	def serializeRequest(self, request, extended=False):
		encoded = self.codec.encodeRequest(request)
		if extended:
			encoded = struct.pack('>Hs', len(encoded), encoded)

		return encoded

	def normalizeRequest(self, request_s, extended=False):
		if extended:
			length, request_s = struct.unpack('>Hs', request_s)
			if length != len(request_s):
				request_s = ''

		return self.codec.decodeRequest(request_s)

	def createRequestString(self, identifier, request_type, request_name, extended=False):
		request = self.request_factory(identifier)
		request.addQuestion(request_type, request_name)

		encoded = self.codec.encodeRequest(request)
		if extended:
			encoded = struct.pack('>Hs', len(encoded), encoded)

		return encoded

	def serializeResponse(self, response, extended=False):
		encoded = self.codec.encodeResponse(response)
		if extended:
			encoded = struct.pack('>Hs', len(encoded), encoded)

		return encoded

	def normalizeResponse(self, response_s, extended=False):
		if extended:
			length, request_s = struct.unpack('>Hs', response_s)
			if length != len(response_s):
				request_s = ''

		return self.codec.decodeResponse(response_s)

Add missing import