python-2.7 - 生成 HTML 页面时出现 KeyError
问题描述
我正在从一个简单的模板生成一个 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'
是什么导致了我的错误,我怎样才能成功解决这个问题?我试图将链接生成为 alist
并将其返回 join ,但它会抛出相同的Exception
. 对此的任何帮助都会很棒
解决方案
想通了,我在 HTML 中有 javascript,所以我们都知道 javascript 使用括号{}
来解析它们:已{{}}
修复。
推荐阅读
- ios - 检测 iOS 应用程序是否在发布版本的调试器中运行
- vue.js - 如何修复 vue js 链接的 facebook 链接预览?
- bash - 如果语句没有给出预期值
- python - 尝试在抓取时单击下一步按钮
- azure-web-app-service - Azure ARM 模板 - 设置应用程序设置和诊断日志设置
- javascript - 如何用另一个对象数组中的一个对象更改对象数组中的对象值?
- c# - 如何为自己的孩子从父抽象继承抽象方法?
- python - 如何使用 %p 在 python 中将包含 AM/PM 的字符串转换为日期时间
- r - 使用 knitr/R Markdown 在目录中换行
- vba - 使用文件夹中的文件 - VBA 例外