您的位置:首页 > 移动开发 > Objective-C

使用pyinstaller打包为可执行程序后, iptables模块执行时提示 AttributeError: 'NoneType' object has no attribute 'throw_exception'

2017-02-07 18:14 1141 查看
问题主要出现在使用python的python-iptables的包时, pip install python-iptables后, 直接python test-iptables.py是正常的

import iptc
table = iptc.Table(iptc.Table.NAT)
for chain in table.chains:
for rule in chain.rules:
for match in rule.matches:
print match.parameters['dport']
print type(match.parameters['dport'])



10005
<type 'unicode'>
10001
<type 'unicode'>
10010
<type 'unicode'>
10008
<type 'unicode'>
10003
<type 'unicode'>
10007
<type 'unicode'>
10012
<type 'unicode'>
10011
<type 'unicode'>
10006
<type 'unicode'>
10002
<type 'unicode'>
10009
<type 'unicode'>
10014
<type 'unicode'>
10004
<type 'unicode'>
10013
<type 'unicode'>


但是当使用pyinstaller -F test-iptables.py 打包成可执行程序, 执行打包好的程序提示


Traceback (most recent call last):
File "test-iptables.py", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/PyInstaller/loader/pyimod03_importers.py", line 389, in load_module
exec(bytecode, module.__dict__)
File "iptc/__init__.py", line 10, in <module>
File "/usr/local/lib/python2.7/dist-packages/PyInstaller/loader/pyimod03_importers.py", line 389, in load_module
exec(bytecode, module.__dict__)
File "iptc/ip4tc.py", line 13, in <module>
File "/usr/local/lib/python2.7/dist-packages/PyInstaller/loader/pyimod03_importers.py", line 389, in load_module
exec(bytecode, module.__dict__)
File "iptc/xtables.py", line 709, in <module>
AttributeError: 'NoneType' object has no attribute 'throw_exception'
Failed to execute script test-iptables


通过堆栈可以看到, File "iptc/xtables.py", line 709, in <module> 没有throw_exception这个属性

通过find / -name xtables.py找到 pip 安装的包路径: /usr/local/lib/python2.7/dist-packages/iptc/xtables.py
查看该文件709行



通过代码得知, _lib_xtwrapper是通过 find_library("xtwrapper")得到的

查看 find_library方法的代码,最终定位到同目录下的 util.py文件中的_do_find_library()方法:



将p的值打印出来,看有什么区别, 在直接python test-iptables.py的情况下 输出:



最终还将拼接上/libxtwrapper.so得到最终路径, 并加载该so

该so的路径在/usr/local/lib/python2.7/dist-packages/libxtwrapper.so, 所以最终可以找到该文件, 没有问题

但是当打包成可执行程序再执行时, 输出如下:



可以看到, sys.path的路径中只有最终释放出的/tmp/目录, 所以自然不会找到libxtwrapper.so从而返回None值, 自然报错

我的解决方法是将libxtwrapper.so当做资源一起打包进可执行程序, 修改一下util.py文件中的_do_find_library()方法源码, 让其可以找到释放出来的libxtwrapper.so文件



目录结构:


root@xbox-virtual-machine:/home/xbox/Desktop/pack# tree
.
├── pack.spec
├── resources
│ ├── x64
│ │ └── libxtwrapper.so
│ └── x86
│ └── libxtwrapper.so
└── test-iptables.py


关于打包可以参考这篇文章: http://www.cnblogs.com/dacainiao/p/5918845.html
打包完成后再次运行没有报错, 功能正常:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐