【发布时间】:2014-03-11 19:13:15
【问题描述】:
我在让这个 python web 脚本运行并在网页上打印结果时遇到了一些麻烦。这是我正在玩的一个小项目,它将在 OpenWRT 路由器上运行。
当无线客户端连接到接入点时,CGI 脚本将针对从 HTTP 会话中检索到的设备 IP 地址运行,并显示是否有任何端口打开,这可能会使个人对公共网络进行某种攻击。
我可以通过wireshark 数据包捕获确认端口扫描正常运行。
此时我不断收到一个 CGI 错误,说明 Nmap 语法存在错误 - 尽管我知道这在 Python shell 中有效
我得到的错误是 -
A problem occurred in a Python script. Here is the sequence of function calls leading up to the error, in the order they occurred.
/Applications/XAMPP/xamppfiles/cgi-bin/test.py in ()
20 for client in nm.scan():
21 print('----------------------------------------------------')
=> 22 print('Host : %i' % (nm[client].all_protocols().keys))
23 """
24
nm = <nmap.nmap.PortScanner object>, client = 'nmap', ].all_protocols undefined
/Applications/XAMPP/xamppfiles/cgi-bin/build/bdist.macosx-10.8-intel/egg/nmap/nmap.py in __getitem__(self=<nmap.nmap.PortScanner object>, host='nmap')
<type 'exceptions.KeyError'>: 'nmap'
args = ('nmap',)
message = 'nmap'
这是我的代码
#!/usr/bin/env python2.7
import cgi
import os
import nmap
import cgitb; cgitb.enable() # for troubleshooting
print "Content-type: text/html"
print
print """
<html>
<head><title>Sample CGI Script</title></head>
<body>
<h3> Sample CGI Script </h3>
"""
client = cgi.escape(os.environ["REMOTE_ADDR"])
print ('your IP Address is: ' + client)
nm = nmap.PortScanner()
nm.scan(client, '21')
for client in nm.scan():
print('----------------------------------------------------')
print('Host : %i' % (nm[client].all_protocols().keys))
"""
</body>
</html>
"""
【问题讨论】:
标签: python python-2.7 cgi web.py nmap