论坛首页 编程语言技术论坛

pickle.PicklingError: Can't pickle <function scan_progressive at 0x01C255B0>:...

浏览 3076 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2011-09-14  
   在安装了python-nmap0.14后,写了一个简单的测试执行失败。
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "D:\opt\Python27\lib\multiprocessing\forking.py", line 373, in main
    prepare(preparation_data)
  File "D:\opt\Python27\lib\multiprocessing\forking.py", line 488, in prepare
    '__parents_main__', file, path_name, etc
  File "E:\Workspace\scanner_01.py", line 13, in <module>
    nma.scan(hosts='192.168.18.0/24', arguments='-sP')
  File "D:\opt\Python27\lib\site-packages\nmap\nmap.py", line 417, in scan
    self._process.start()
  File "D:\opt\Python27\lib\multiprocessing\process.py", line 130, in start
    self._popen = Popen(self)
  File "D:\opt\Python27\lib\multiprocessing\forking.py", line 271, in __init__
    dump(process_obj, to_child, HIGHEST_PROTOCOL)
  File "D:\opt\Python27\lib\multiprocessing\forking.py", line 193, in dump
    ForkingPickler(file, protocol).dump(obj)
  File "D:\opt\Python27\lib\pickle.py", line 224, in dump
    self.save(obj)
  File "D:\opt\Python27\lib\pickle.py", line 331, in save
    self.save_reduce(obj=obj, *rv)
  File "D:\opt\Python27\lib\pickle.py", line 419, in save_reduce
    save(state)
  File "D:\opt\Python27\lib\pickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self
  File "D:\opt\Python27\lib\pickle.py", line 649, in save_dict
    self._batch_setitems(obj.iteritems())
  File "D:\opt\Python27\lib\pickle.py", line 681, in _batch_setitems
    save(v)
  File "D:\opt\Python27\lib\pickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self
  File "D:\opt\Python27\lib\pickle.py", line 747, in save_global
    (obj, module, name))
pickle.PicklingError: Can't pickle <function scan_progressive at 0x01C255B0>: it
's not found as nmap.nmap.scan_progressive

  代码如下:
import nmap
nma = nmap.PortScannerAsync()

def callback_result(host, scan_result):
    print '------------------'
    print host, scan_result

nma.scan(hosts='192.168.18.0/24', arguments='-sP')

while nma.still_scanning():
    print("Waiting ...")
    nma.wait(2) 

起初以为代码写错了,然后我找到nmap安装目录下的example.py执行,发生同样的错误。经过反复跟踪,发现在pickle.py文件745行抛出了错误:
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "D:\opt\Python27\lib\site-packages\nmap\nmap.py", line 413, in scan
    target=self.scan_progressive,
AttributeError: 'PortScannerAsync' object has no attribute 'scan_progressive'

然后又阅读nmap的源码nmap.py,终于发现在402行,方法scan_progressive被定义在scan方法内部,导致pickle失败。

解决方法:
    将方法Scan_progressive定义到外面,并将nmap.py 413行从
target=scan_progressive,
改为:
target=self.progressive,
即可。


总结:不晓得有木有其他的影响,但是得出一个结论,别人的东西,不能尽信,是钢是铁,试了才晓得。。。。


论坛首页 编程语言技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics