我有一个类似于以下内容的输出类(已编辑):
from colorclass import Color
from colorclass import disable_all_colors, enable_all_colors, is_enabled
from time import localtime, strftime
from ApplicationFiles.lib.core.__version__ import __version__
from enum import IntEnum
class OutputHelper(object):
def __init__(self, arguments, app):
if arguments.nocolor:
disable_all_colors()
self.domain = "undefined"
self.severity_print = arguments.severity
self.verbose = arguments.verbose
self.silent = arguments.silent
self.seperator = "=============================================="
self.modules_count = app.modules_count()
# this is here to allow easier redirection of output to other
# interfaces later on in the project
def write(self, severity, message):
self.terminal(severity, message)
def terminal(self, severity, message):
if severity == 0 and not self.verbose:
return
if severity < self.severity_print:
return
formatting = {
0: Color('{autoblue}[VERBOSE]{/autoblue}')
}
leader = formatting.get(severity, '[#]')
format_args = {
'time': strftime("%H:%M:%S", localtime()),
'domain': self.domain,
'leader': leader,
'message': message,
}
template = '[{time}] [{domain}] {leader} {message}'
print(template.format(**format_args))
class Level(IntEnum):
VERBOSE = 0
我正在从文件中读取域列表,并将其通过此类传递给以下内容:
for domain in arguments.domain_list:
domains.append(domain)
for domain in domains:
output.domain = domain
app.modules['example_module'].run(arguments, output)
然后依次读取:
def run(arguments, output):
output.write(Level.VERBOSE, 'TEST DOMAIN')
无论出于何种原因,这都会在每次打印后打印换行符。例如:
1 modules loaded
==============================================
[16:57:25] [testone.com
] [VERBOSE] TEST DOMAIN
[16:57:25] [testtwo.com
] [VERBOSE] TEST DOMAIN
[16:57:25] [testthree.com
] [VERBOSE] TEST DOMAIN
应显示为:
1 modules loaded
==============================================
[16:57:25] [testone.com] [VERBOSE] TEST DOMAIN
[16:57:25] [testtwo.com] [VERBOSE] TEST DOMAIN
[16:57:25] [testthree.com] [VERBOSE] TEST DOMAIN
我知道我在处理对象时出错,或者我在某个地方如何引用它们-但是我很努力地看到它。我不明白什么?在TerminalOutput()函数中处理此问题似乎很愚蠢,我宁愿在存储域时对其进行排序。
正在使用以下文件读取文件:
def readable_file(parser, arg):
if not os.path.exists(arg):
parser.error("The file %s does not exist!" % arg)
else:
return open(arg, 'r') # return an open file handle
在argparse类中使用以下命令调用
domains.add_argument(
'-dL', dest='domain_list', required=False,
help='Specify a list of target domain names.',
metavar="FILE",
type=lambda x: CliFileHelper.readable_file(parser, x)
)
答案 0 :(得分:4)
您没有向我们展示如何阅读域,而是使用:
for domain in domainFileDescriptor:
domains.append(domain)
#or
domains=domainFileDescriptor.readlines()
将从文件中读取的每一行的末尾保留换行符。您需要剥离它们:
for domain in domainFileDescriptor:
domains.append(domain.strip())
提示是换行符的位置(在域之后)。注意:在这种情况下,文件描述符和句柄是同一回事。