如何使用各种可选参数弹出

问题描述

我有一个过程需要在python中运行并获取输出。我如何运行它,以便用户可以指定可以在进程上运行的可选参数。到目前为止,这就是我的功能。


    async def analyze_target(self,target,batchSize,delay,maxdepth,maxurls,maxwait,recursive,useragent,htmlmaxcols,htmlmaxrows):

        cmd = "wappalyzer"
        if batchSize != "":
            cmd = cmd + " --batch-size=" + batchSize

        if delay != "":
            cmd = cmd + " --delay=" + delay

        if maxdepth != "":
            cmd = cmd + " --max-depth=" + maxdepth

        if maxurls != "":
            cmd = cmd + " --max-urls=" + maxurls

        if maxwait != "":
            cmd = cmd + " --max-wait=" + maxwait

        if recursive == True:
            cmd = cmd + " --recursive"

        if useragent != "":
            cmd = cmd + " --user-agent=" + useragent

        if htmlmaxcols != "":
            cmd = cmd + " --html-max-cols=" + htmlmaxcols

        if htmlmaxrows != "":
            cmd = cmd + " --html-max-rows=" + htmlmaxrows

        cmd = cmd + " " + target
        
        p = subprocess.Popen(cmd,stdout=subprocess.PIPE,stderr=subprocess.STDOUT,shell=True)
        
        tmp = p.stdout.read()

        self.logger.info(tmp)

        p_status = p.wait()

        """
        Returns log of what was wappalyzed
        """
        message = f"target {target} has been wappalyzed with output {tmp}"

        # This logs to the docker logs
        self.logger.info(message)
        return tmp

解决方法

Popen的第一个参数args支持单个字符串或序列。因此,您可以在list后面附加可选参数

cmd = [cmd,*args]

或者您也可以在函数的参数中使用** kwargs(dictionary displays)(基本上将任何命名的参数视为字典),并执行以下操作:

def analyze_target(...,**kwargs):
    ...

    for key,value,in kwargs.items():
        cmd += f" --{key.replace('_','-')}={value}"  # change name since dashes cannot be used as names

    ...

# would look like this:
analyze_target(...,additional_argument='test')

# cmd: all other arguments + --additional-argument=test

,

不构建字符串。只需构建一个包含命令名称及其参数作为单独元素的列表即可。

async def analyze_target(self,target,batchSize,delay,maxdepth,maxurls,maxwait,recursive,useragent,htmlmaxcols,htmlmaxrows):

    cmd = ["wappalyzer"]
    if batchSize != "":
        cmd.append("--batch-size=" + batchSize)

    if delay != "":
        cmd.append("--delay=" + delay)

    if maxdepth != "":
        cmd.append("--max-depth=" + maxdepth)

    if maxurls != "":
        cmd.append("--max-urls=" + maxurls)

    if maxwait != "":
        cmd.append("--max-wait=" + maxwait)

    if recursive:
        cmd.append("--recursive")

    if useragent != "":
        cmd.append("--user-agent=" + useragent)

    if htmlmaxcols != "":
        cmd.append("--html-max-cols=" + htmlmaxcols)

    if htmlmaxrows != "":
        cmd.append("--html-max-rows=" + htmlmaxrows)

    cmd.append(target)
    
    # Drop the shell=True,so that command is executed
    # directly,without shell involvement
    p = subprocess.Popen(cmd,stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
    
    ...

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...