Python 错误“dict”对象不可调用
我是 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
我期望的输出: 输出图片
I'm new in python And I have a problem that I haven't been able to solve for a couple of days and I don't understand what the error is at all.
I am writing a program based on the Nmap program and the library of the same name, the functionality of which is to scan open ports of some list of ip addresses.
I'm trying to implement this through a dictionary, when I did everything on a static variable of ports, everything was fine, but when the program scans ports for each address, such an error is issued on the second iteration of the scan cycle.
'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)
Below is the function code.
You can also install the library with this command: 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
Output that i expect:
pic of output
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
未修改的
scan_command
方法返回 xml.etree.ElementTree.Element 表示 XML 树的对象,如下所示:这是获取
protocol
、portid
和其中的状态
:(也可在https://gist.githubusercontent.com/md2perpe/5cca3471e3f866e77915796197d1e070/raw/cfdab2d99926030a61201748b3ec2cbbc429b7cf/main.py)
The unmodified
scan_command
method returns an xml.etree.ElementTree.Element object representing an XML tree that looks like this:Here's code to get the
protocol
,portid
andstate
from it:(Also available at https://gist.githubusercontent.com/md2perpe/5cca3471e3f866e77915796197d1e070/raw/cfdab2d99926030a61201748b3ec2cbbc429b7cf/main.py)