Python 错误 'dict' 对象不可调用
Python error 'dict' object is not callable
我是 python 的新人,我遇到了一个问题,几天都无法解决,我根本不明白错误是什么。
我正在编写一个基于 Nmap 程序和同名库的程序,其功能是扫描一些 ip 地址列表的开放端口。
我正在尝试通过字典来实现这一点,当我在端口的静态变量上执行所有操作时,一切都很好,但是当程序扫描每个地址的端口时,在扫描周期的第二次迭代时会发出这样的错误。
'dict' object is not callable
File "C:\Users\Duoksi\Desktop\codes\python\NmapScanPy\test.py", line 47, in ScanPorts
result = nmap.scan_command(ip, arg = attribute)
下面是功能代码。
您还可以使用以下命令安装库:pip install python3-nmap
import nmap3
def Portscan():
nmap = nmap3.Nmap()
attribute = "-p "
port = input("Enter ports(ex. 80,443)\n")
attribute += str(port)
ipport = dict()
IPrange = ['93.187.72.82', '93.187.72.94', '93.187.72.115', '93.187.72.241', '93.187.72.19', '93.187.72.59', '93.187.72.208',
'93.187.72.179', '93.187.72.24', '93.187.72.137', '93.187.72.136', '93.187.72.130']
for ip in IPrange:
result = nmap.scan_command(ip, arg = attribute)
resultclean = re.findall('\'protocol\': \'\w+\', \'portid\': \'\w+\', \'state\': \'\w+\'', str(result))
for res in resultclean:
ipport.setdefault(ip, []).append(res)
return ipport
我期望的输出:
pic of output
未修改的 scan_command
方法 returns 表示 XML 树的 xml.etree.ElementTree.Element 对象,如下所示:
<nmaprun scanner="nmap" args="/usr/bin/nmap -oX - -p 80 93.187.72.82" start="1648151628" startstr="Thu Mar 24 20:53:48 2022" version="7.80" xmloutputversion="1.04">
<scaninfo type="connect" protocol="tcp" numservices="1" services="80" />
<verbose level="0" />
<debugging level="0" />
<host starttime="1648151628" endtime="1648151629">
<status state="up" reason="syn-ack" reason_ttl="0" />
<address addr="93.187.72.82" addrtype="ipv4" />
<hostnames>
</hostnames>
<ports>
<port protocol="tcp" portid="80">
<state state="open" reason="syn-ack" reason_ttl="0" />
<service name="http" method="table" conf="3" />
</port>
</ports>
<times srtt="64487" rttvar="50416" to="266151" />
</host>
<runstats>
<finished time="1648151629" timestr="Thu Mar 24 20:53:49 2022" elapsed="1.28" summary="Nmap done at Thu Mar 24 20:53:49 2022; 1 IP address (1 host up) scanned in 1.28 seconds" exit="success" />
<hosts up="1" down="0" total="1" />
</runstats>
</nmaprun>
这是从中获取 protocol
、portid
和 state
的代码:
import nmap3
IP_RANGE = [
'93.187.72.82', '93.187.72.94', '93.187.72.115', '93.187.72.241', '93.187.72.19', '93.187.72.59',
'93.187.72.208', '93.187.72.179', '93.187.72.24', '93.187.72.137', '93.187.72.136', '93.187.72.130',
]
def scan_one_ip(ip: str, port: str) -> list[str]:
result = nmap3.Nmap().scan_command(ip, arg=f"-p {port}")
port_element = result.find("./host/ports/port")
return [
port_element.get("protocol"),
port_element.get("portid"),
port_element.find("state").get("state"),
]
def scan_port(port: str) -> dict[str, list[str]]:
return {ip: scan_one_ip(ip, port) for ip in IP_RANGE}
def main():
port = input("Enter ports (ex. 80,443)\n")
print(scan_port(port))
if __name__ == '__main__':
main()
我是 python 的新人,我遇到了一个问题,几天都无法解决,我根本不明白错误是什么。 我正在编写一个基于 Nmap 程序和同名库的程序,其功能是扫描一些 ip 地址列表的开放端口。 我正在尝试通过字典来实现这一点,当我在端口的静态变量上执行所有操作时,一切都很好,但是当程序扫描每个地址的端口时,在扫描周期的第二次迭代时会发出这样的错误。
'dict' object is not callable
File "C:\Users\Duoksi\Desktop\codes\python\NmapScanPy\test.py", line 47, in ScanPorts
result = nmap.scan_command(ip, arg = attribute)
下面是功能代码。 您还可以使用以下命令安装库:pip install python3-nmap
import nmap3
def Portscan():
nmap = nmap3.Nmap()
attribute = "-p "
port = input("Enter ports(ex. 80,443)\n")
attribute += str(port)
ipport = dict()
IPrange = ['93.187.72.82', '93.187.72.94', '93.187.72.115', '93.187.72.241', '93.187.72.19', '93.187.72.59', '93.187.72.208',
'93.187.72.179', '93.187.72.24', '93.187.72.137', '93.187.72.136', '93.187.72.130']
for ip in IPrange:
result = nmap.scan_command(ip, arg = attribute)
resultclean = re.findall('\'protocol\': \'\w+\', \'portid\': \'\w+\', \'state\': \'\w+\'', str(result))
for res in resultclean:
ipport.setdefault(ip, []).append(res)
return ipport
我期望的输出: pic of output
未修改的 scan_command
方法 returns 表示 XML 树的 xml.etree.ElementTree.Element 对象,如下所示:
<nmaprun scanner="nmap" args="/usr/bin/nmap -oX - -p 80 93.187.72.82" start="1648151628" startstr="Thu Mar 24 20:53:48 2022" version="7.80" xmloutputversion="1.04">
<scaninfo type="connect" protocol="tcp" numservices="1" services="80" />
<verbose level="0" />
<debugging level="0" />
<host starttime="1648151628" endtime="1648151629">
<status state="up" reason="syn-ack" reason_ttl="0" />
<address addr="93.187.72.82" addrtype="ipv4" />
<hostnames>
</hostnames>
<ports>
<port protocol="tcp" portid="80">
<state state="open" reason="syn-ack" reason_ttl="0" />
<service name="http" method="table" conf="3" />
</port>
</ports>
<times srtt="64487" rttvar="50416" to="266151" />
</host>
<runstats>
<finished time="1648151629" timestr="Thu Mar 24 20:53:49 2022" elapsed="1.28" summary="Nmap done at Thu Mar 24 20:53:49 2022; 1 IP address (1 host up) scanned in 1.28 seconds" exit="success" />
<hosts up="1" down="0" total="1" />
</runstats>
</nmaprun>
这是从中获取 protocol
、portid
和 state
的代码:
import nmap3
IP_RANGE = [
'93.187.72.82', '93.187.72.94', '93.187.72.115', '93.187.72.241', '93.187.72.19', '93.187.72.59',
'93.187.72.208', '93.187.72.179', '93.187.72.24', '93.187.72.137', '93.187.72.136', '93.187.72.130',
]
def scan_one_ip(ip: str, port: str) -> list[str]:
result = nmap3.Nmap().scan_command(ip, arg=f"-p {port}")
port_element = result.find("./host/ports/port")
return [
port_element.get("protocol"),
port_element.get("portid"),
port_element.find("state").get("state"),
]
def scan_port(port: str) -> dict[str, list[str]]:
return {ip: scan_one_ip(ip, port) for ip in IP_RANGE}
def main():
port = input("Enter ports (ex. 80,443)\n")
print(scan_port(port))
if __name__ == '__main__':
main()