python - 使用传递给函数的变量从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-nmap3
repo 中获取用户对他想要的扫描类型的输入,并将其用作result = f'nmap.nmapType(address)'
行中的变量。
但是,如果我删除 f 字符串,则将变量视为类的python
方法。我想要的是用户能够在变量中指定方法名称。nmapType
nmap
nmapType
有没有办法解决这个问题并使用用户输入nmap
根据用户输入调用正确的方法?
我希望用户输入扫描类型,将其传递给nmap_scan
函数,并使用它来执行扫描。
解决方案
推荐阅读
- java - 在 Java 中使用 Skaffold
- javascript - 如何将非模块(!)JavaScript 导入 Polymer 3.0
- python - 如何在终止时执行代码
- python - sqlite3.sqlite3.OperationalError:数据库表在 django LiveServerTestCase 上被锁定
- netty - 如何设置netty 4线程名称?
- laravel-5 - 在 Laravel 中使用 HasManyThrough
- android - 从堆栈中打开最旧的活动实例并清除它上面的所有内容。可以使用一些有意图的标志来管理它吗?
- python - 在按下键时记录鼠标单击并在python中释放相同的键时停止记录
- java - 使用Java中的Spark数据框在db中持久化窗口函数的输出
- javascript - 一段时间后如何调用particleJS?