更简单的方法来编写这个辅助功能?

时间:2017-09-17 20:09:22

标签: python python-2.7

我有一个程序会在给定主机上启动nmap扫描,添加我拥有的参数:

def _get_all_info(self):
    """
    get all the information from the scan
    """

    def __add_options():
        option_list = []
        if self.opts is not None:
            for opt in self.opts:
                if opt not in lib.attacks.nmap_scan.nmap_opts:
                    logger.warning(set_color(
                        "option '{}' is not a valid nmap option "
                        "skipping...".format(opt)
                    ))
                else:
                    option_list.append(opt)
        return " ".join(options_list)

    scanned_data = self.NM.scan(self.ip, ports=self.ports, arguments=__add_options())
    if self.pretty:
        scanned_data = json.dumps(scanned_data, indent=4, sort_keys=True)
    return scanned_data

我可以更轻松地编写__add_options部分吗?

1 个答案:

答案 0 :(得分:1)

使用list comprehensions并删除无用的日志记录:

def __add_options():
    return " ".join([opt for opt in self.opts if opt in lib.attacks.nmap_scan.nmap_opts])