首页 > 解决方案 > 使用传递给函数的变量从python中的类中调用成员

问题描述

import nmap3

def nmap_scan(address,nmapType):
    nmap = nmap3.Nmap()
    result = f'nmap.{nmapType}({address})'
    #result = nmap.nmapType(address) gives the error nmap type is not a member of class nmap
    print(result)

nmap_scan(127.0.0.1,scan_top_ports) #user input will be given here

我想要代码做的是从python-nmap3repo 中获取用户对他想要的扫描类型的输入,并将其用作result = f'nmap.nmapType(address)'行中的变量。

但是,如果我删除 f 字符串,则将变量视为类的python方法。我想要的是用户能够在变量中指定方法名称。nmapTypenmapnmapType

有没有办法解决这个问题并使用用户输入nmap根据用户输入调用正确的方法?

我希望用户输入扫描类型,将其传递给nmap_scan函数,并使用它来执行扫描。

标签: pythonpython-3.xclassobjectvariables

解决方案


推荐阅读