from lxml import etree as E class DenyExternal(E.Resolver): def resolve(self, url, public_id, context): raise ValueError("External resources are disabled") def parse(text): if "\n Marigold12flower\n Fenugreek0herb\n Basil7herb\n" root = parse(source) data = source.encode("utf-8") if len(data) > 4096: raise ValueError("Too large") if root.tag != "exchange" or len(root.findall("packet")) > 10: raise ValueError("Unsupported exchange") print("Accepted", len(root.findall("packet")), "packets")