介绍Python编译与反编译。

源码

test_compile.py 源码

def main():
    print "Hello World!"

if __name__ == '__main__':
    main()

编译 py -> pyc

[root@xiexianbin_cn python]# python -m py_compile test_compile.py
[root@xiexianbin_cn python]# ls
test_compile.py  test_compile.pyc
[root@xiexianbin_cn python]# hexdump -C test_compile.pyc 
00000000  03 f3 0d 0a 94 cf 29 58  63 00 00 00 00 00 00 00  |......)Xc.......|
00000010  00 02 00 00 00 40 00 00  00 73 23 00 00 00 64 00  |.....@...s#...d.|
00000020  00 84 00 00 5a 00 00 65  01 00 64 01 00 6b 02 00  |....Z..e..d..k..|
00000030  72 1f 00 65 00 00 83 00  00 01 6e 00 00 64 02 00  |r..e......n..d..|
00000040  53 28 03 00 00 00 63 00  00 00 00 00 00 00 00 01  |S(....c.........|
00000050  00 00 00 43 00 00 00 73  09 00 00 00 64 01 00 47  |...C...s....d..G|
00000060  48 64 00 00 53 28 02 00  00 00 4e 73 0c 00 00 00  |Hd..S(....Ns....|
00000070  48 65 6c 6c 6f 20 57 6f  72 6c 64 21 28 00 00 00  |Hello World!(...|
00000080  00 28 00 00 00 00 28 00  00 00 00 28 00 00 00 00  |.(....(....(....|
00000090  73 0f 00 00 00 74 65 73  74 5f 63 6f 6d 70 69 6c  |s....test_compil|
000000a0  65 2e 70 79 74 04 00 00  00 6d 61 69 6e 01 00 00  |e.pyt....main...|
000000b0  00 73 02 00 00 00 00 01  74 08 00 00 00 5f 5f 6d  |.s......t....__m|
000000c0  61 69 6e 5f 5f 4e 28 02  00 00 00 52 00 00 00 00  |ain__N(....R....|
000000d0  74 08 00 00 00 5f 5f 6e  61 6d 65 5f 5f 28 00 00  |t....__name__(..|
000000e0  00 00 28 00 00 00 00 28  00 00 00 00 73 0f 00 00  |..(....(....s...|
000000f0  00 74 65 73 74 5f 63 6f  6d 70 69 6c 65 2e 70 79  |.test_compile.py|
00000100  74 08 00 00 00 3c 6d 6f  64 75 6c 65 3e 01 00 00  |t....<module>...|
00000110  00 73 04 00 00 00 09 03  0c 01                    |.s........|
0000011a
[root@xiexianbin_cn python]# 

安装uncompyle2

[root@xiexianbin_cn python]# pip install uncompyle2
Collecting uncompyle2
  Downloading http://pypi.doubanio.com/packages/ca/ca/8ee6e0adba3406cee2536fcbb804380cb3a644fccbaa638dbfff336d901f/uncompyle2-2.0.0-py2.py3-none-any.whl
Collecting uncompyle6 (from uncompyle2)
  Downloading http://pypi.doubanio.com/packages/75/15/af5c582223005eaf88385335f2a3f94795c272268490273527c918e8706e/uncompyle6-2.9.6-py2.py3-none-any.whl (146kB)
    100% |████████████████████████████████| 153kB 4.7MB/s 
Collecting spark-parser<1.5.0,>=1.4.0 (from uncompyle6->uncompyle2)
  Downloading http://pypi.doubanio.com/packages/29/b5/085e5239fce782124f441f90268086a824c92d7644d7fbd04a442442d298/spark_parser-1.4.3-py2-none-any.whl
Collecting xdis<3.3.0,>=3.2.3 (from uncompyle6->uncompyle2)
  Downloading http://pypi.doubanio.com/packages/af/c8/1046f3187bf6ec4aa355d2020a00fda29f696eb73b5c7b7cbdb6540328bf/xdis-3.2.3-py2.py3-none-any.whl (64kB)
    100% |████████████████████████████████| 71kB 10.0MB/s 
Installing collected packages: spark-parser, xdis, uncompyle6, uncompyle2
Successfully installed spark-parser-1.4.3 uncompyle2-2.0.0 uncompyle6-2.9.6 xdis-3.2.3
You are using pip version 8.1.2, however version 9.0.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
[root@xiexianbin_cn python]# 

反编译 pyc -> py

[root@xiexianbin_cn python]# uncompyle6 test_compile.pyc > s.py
[root@xiexianbin_cn python]# cat s.py 
# uncompyle6 version 2.9.6
# Python bytecode 2.7 (62211)
# Decompiled from: Python 2.7.5 (default, Sep 15 2016, 22:37:39) 
# [GCC 4.8.5 20150623 (Red Hat 4.8.5-4)]
# Embedded file name: test_compile.py
# Compiled at: 2016-11-14 22:52:04


def main():
    print 'Hello World!'


if __name__ == '__main__':
    main()
# okay decompiling test_compile.pyc
[root@xiexianbin_cn python]# 

完毕。