获取错误:attributeError:模块' nmap'没有属性' portscanner'

发布于 2025-01-17 13:42:25 字数 822 浏览 5 评论 0原文

我一直在寻找如何解决这个Python问题的答案:

属性错误:模块“nmap”没有属性“PortScanner”

我想了解有关端口扫描的更多信息,但我什至无法在我正在使用的 Visual Studio Code 上安装该模块。我已经尝试了我和许多人能想到的一切:

  1. 卸载并重新安装 python-nmap 以及 nmap (因为它们是互连的)。
  2. 我尝试过重命名模块本身。
  3. 我已经在不同的 IDE 上启动了我的代码,
  4. 我创建了一个单独的文件夹并将模块和我的项目放在那里。

到目前为止没有成功..

这是我的代码:

import nmap


nm = nmap.PortScanner()
nm.scan('127.0.0.1', '22-443')

和输出:

/usr/local/bin/python3 /Users
/user2132/Desktop/PYTHONProjects/portscannning.py
Traceback (most recent call last):
File "/Users/user2132/Desktop/PYTHONProjects/portscannning.py", line 3, in <module>
nm = nmap.PortScanner()
AttributeError: module 'nmap' has no attribute 'PortScanner'

接下来我可以尝试什么?

PS我使用的是MacOS

I have been seeking for answers of how to fix this Python problem:

AttributeError: module 'nmap' has no attribute 'PortScanner'

I wanted to learn more about port-scanning but I couldn't even install the module on Visual Studio Code, which I am using. I've tried everything that I and many people can think of:

  1. Uninstalled and reinstalled python-nmap as well as just nmap (since they are interconnected).
  2. I've tried renaming the module itself.
  3. I've launched my code on different IDEs
  4. I've created a separate folder and put modules and my project there.

No success so far..

This is my code:

import nmap


nm = nmap.PortScanner()
nm.scan('127.0.0.1', '22-443')

and the output:

/usr/local/bin/python3 /Users
/user2132/Desktop/PYTHONProjects/portscannning.py
Traceback (most recent call last):
File "/Users/user2132/Desktop/PYTHONProjects/portscannning.py", line 3, in <module>
nm = nmap.PortScanner()
AttributeError: module 'nmap' has no attribute 'PortScanner'

What can I try next?

P.S. I am using MacOS

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

灵芸 2025-01-24 13:42:25

我能够重现该错误。问题出在 nmap 库上。 pip install nmap 安装 nmap python 库,但 python-nmap 需要 nmap 二进制文件,而且 nmap python 库与 python-nmap 冲突,因为它们共享相同的模块名称。正确的nmap可以从Nmap的官方下载页面安装,

请按照步骤操作步骤

1. 卸载库

pip uninstall nmap
pip uninstall python-nmap

步骤 2. 安装 python-nmap

pip install python-nmap

步骤 3. 检查 nmap 是否已安装到您的系统中

which nmap

步骤 3. 如果已安装,请继续下一步步骤,如果没有:

转至Nmap官方下载页面,下载并安装适合您操作系统的nmap。

请确保在安装过程中选择了添加到PATH选项。

步骤 4. 重新启动系统(重新启动计算机)

在终端中使用 which nmap 命令检查 nmap 安装。


之后您可以检查PortScanner是否在nmap中。

import nmap
dir(nmap)

返回

['ET',
 'PortScanner', <=== IS HERE!
 'PortScannerAsync',
 'PortScannerError',
 'PortScannerHostDict',
 'PortScannerTimeout',
 'PortScannerYield',
 'Process',
 '__author__',
 '__builtins__',
 '__cached__',
 '__doc__',
 '__file__',
 '__last_modification__',
 '__loader__',
 '__name__',
 '__package__',
 '__path__',
 '__spec__',
 '__version__',
 'convert_nmap_output_to_encoding',
 'csv',
 'io',
 'nmap',
 'os',
 're',
 'shlex',
 'subprocess',
 'sys']

最终测试

import nmap
nm = nmap.PortScanner()
nm.scan('127.0.0.1', '22-443')

返回

{'nmap': {'command_line': 'nmap -oX - -p 22-443 -sV 127.0.0.1',
  'scaninfo': {'tcp': {'method': 'syn', 'services': '22-443'}},
  'scanstats': {'timestr': 'Tue Mar 29 15:07:02 2022',
   'elapsed': '7.82',
   'uphosts': '1',
   'downhosts': '0',
   'totalhosts': '1'}},
...

I was able to reproduce the error. The problem was with the nmap library. pip install nmap installs nmap python library but python-nmap requires nmap binary, moreover nmap python library conflicts with python-nmap because they share same module name. The correct nmap could be installed from Nmap's official download page

Please follow the steps below:

Step 1. uninstall libraries

pip uninstall nmap
pip uninstall python-nmap

Step 2. install python-nmap

pip install python-nmap

Step 3. Check if nmap installed into your system

which nmap

Step 3. If it is installed, continue to the next step, if not:

Go to Nmap's official download page, download and install nmap for your OS.

Please make sure that add to PATH option is selected during installation.

Step 4. Reboot your system (restart computer)

Check nmap installation with which nmap command in terminal.


After that you can check if PortScanner is in nmap.

import nmap
dir(nmap)

Returns

['ET',
 'PortScanner', <=== IS HERE!
 'PortScannerAsync',
 'PortScannerError',
 'PortScannerHostDict',
 'PortScannerTimeout',
 'PortScannerYield',
 'Process',
 '__author__',
 '__builtins__',
 '__cached__',
 '__doc__',
 '__file__',
 '__last_modification__',
 '__loader__',
 '__name__',
 '__package__',
 '__path__',
 '__spec__',
 '__version__',
 'convert_nmap_output_to_encoding',
 'csv',
 'io',
 'nmap',
 'os',
 're',
 'shlex',
 'subprocess',
 'sys']

Final test

import nmap
nm = nmap.PortScanner()
nm.scan('127.0.0.1', '22-443')

Returns

{'nmap': {'command_line': 'nmap -oX - -p 22-443 -sV 127.0.0.1',
  'scaninfo': {'tcp': {'method': 'syn', 'services': '22-443'}},
  'scanstats': {'timestr': 'Tue Mar 29 15:07:02 2022',
   'elapsed': '7.82',
   'uphosts': '1',
   'downhosts': '0',
   'totalhosts': '1'}},
...
最终幸福 2025-01-24 13:42:25

尝试Ctrl+Shift+P,类型Env,然后创建一个新的环境,然后添加py -3 -m venv .venv .venv,然后按Enter。

Try Ctrl+Shift+P, type env, then create a new environment and add py -3 -m venv .venv and press Enter.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文