生成HTML页面时的KeyError

问题描述 投票:0回答:1

我正在从一个简单的模板生成一个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}

我如何创建标记是通过使用简单类生成一个简单的字符串:

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并返回它加入,但它抛出相同的Exception。对此的任何帮助都会很棒

python-2.7 html-generation
1个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.