将 nmap 导入 python 时出错

Error import nmap into python

我为 python 安装了 nmap 模块。 当我导入 nmap 时,它收到错误。此错误为属性错误。

命令行:

root@harun:~/Desktop# python nmap.py

Traceback (most recent call last):
   File "nmap.py", line 2, in <module>
    import nmap
   File "/root/Desktop/nmap.py", line 3, in <module>
    nm = nmap.PortScanner()
AttributeError: 'module' object has no attribute 'PortScanner'

此代码是:

!/usr/bin/env python

import nmap
nm = nmap.PortScanner()

我更改了代码:

!/usr/bin/env python

from nmap import nmap
nm = nmap.PortScanner()

但是它收到了同样的错误。

又改了一个:

!/usr/bin/env python
 import nmap
 directory=dir(nmap)
 print directory

收到:

 root@harun:~/Desktop# python nmap.py
 ['__builtins__', '__doc__', '__file__', '__name__', '__package__', 'nmap']
 ['__builtins__', '__doc__', '__file__', '__name__', '__package__', 'directory', 'nmap']

但通常情况下:

 root@harun:~# python
 >>> dir(nmap)

['PortScanner', 'PortScannerAsync', 'PortScannerError', 'PortScannerHostDict', 'PortScannerYield', 'Process', '__author__', '__builtins__', '__doc__', '__file__', '__last_modification__', '__name__', '__package__', '__path__', '__version__', 'collections', 'convert_nmap_output_to_encoding', 'csv', 'io', 'nmap', 'os', 're', 'shlex', 'string', 'subprocess', 'sys', 'types', 'xml']

你能告诉我我是怎么做的吗?

我认为评论中的 link('module' 对象没有属性 'Serial')– 是相关的。因此,请尝试 from nmap import PortScanner 而不是 import nmap,然后 nm = PortScanner

您正在调用您的程序 nmap.py 的事实可能在将来引起冲突,因此您可能需要重命名它。