生成 HTML 页面时出现 KeyError

KeyError while generating an HTML page

我正在从一个简单的模板生成一个 HTML 页面:

<div id="dateReference">
    <h1 id="currentDate">Exploit automation completed on: {date_of_attack}</h1>
</div>
<div id="amountOfHost">
    <h4 id="hostCount">Total of {amount_of_hosts} separate hosts attacked</h4>
</div>
<div id="hostDropDown" class="dropdown-content">
    {all_hosts_attacked}
</div>
<div id="successfulAttacks">
    <h4 id="successAttacksBanner">Successful Attacks:</h4>
</div>
{exploit_table}

我创建标签的方式是使用简单的 class:

生成一个简单的字符串
import os
import datetime

import lib.settings


class HtmlPageGenerator(object):

    def __init__(self, successes, failures, host_count):
        self.success = successes
        self.fails = failures
        self.host_count = host_count
        self.html_template = lib.settings.HTML_PAGE_TEMPLATE
        self.attacked_hosts_list = open(lib.settings.HOST_FILE).readlines()

    def _generate_html_table(self, headers):
        retval = '<table id="generatedExploitTable"><tr>'
        for header in headers:
            retval += "<th>{}</th>".format(header)
        retval += "</tr><tr>"
        for value in self.success:
            retval += "<td>{}</td>".format(value)
        retval += "</tr></table>"
        return retval

    def _generate_drop_down_menu(self):
        retval = ""
        for host in self.attacked_hosts_list:
            retval += '<a href="#">{}</a>'.format(host.strip())
        return retval

    def generator(self):
        if not os.path.exists(lib.settings.HTML_PAGE_PATH):
            os.makedirs(lib.settings.HTML_PAGE_PATH)
        with open(self.html_template, 'r') as template, open(lib.settings.HTML_PAGE_GENERATION_FILE_PATH, 'a+') as out:
            to_format = template.read()
            out.write(
                to_format.format(
                    date_of_attack=str(datetime.datetime.today()).split(".")[0],
                    exploit_table=self._generate_html_table(["Exploit Paths"]),
                    amount_of_hosts=self.host_count,
                    all_hosts_attacked=self._generate_drop_down_menu()
                )
            )
        return lib.settings.HTML_PAGE_GENERATION_FILE_PATH

_generate_html_table 函数运行正常,并已成功生成,问题是当我尝试生成 <a href="#" 标签时它抛出以下错误:

Traceback (most recent call last):
  File "xxxxx.py", line 10, in <module>
    23
  File "/Users/admin/bin/tools/xxxxx/lib/page_generator.py", line 42, in generator
    all_hosts_attacked=self._generate_drop_down_menu()
KeyError: '\n            document'

是什么导致了我的错误,我该如何成功解决这个问题?我试图将链接生成为 list 和 return,但它会抛出相同的 Exception。任何帮助都会很棒

弄清楚了,我在 HTML 中有 javascript,所以我们都知道 javascript 使用方括号 {} 来解析它们:{{}}固定。