将pyc文件编码还原
今天本来是要将.py文件转换成.pyc文件,使用py_complie进行的转换,以防止有人误改了原代码,但不巧的是在操作过程中,致使一个主.py文件出现了乱码。如下图:
当时就傻了,这要重写一遍很麻烦。看了一下论坛,可使用uncompyle6反编译.pyc文件。
1.安装uncompyle6
打开powershell, 使用pip自动安装
PS C:\Users\Linyu> pip install uncompyle6
Collecting uncompyle6
Downloading https://files.pythonhosted.org/packages/3e/80/1d1c92b60a17a547fbe9368050107183d096e386a5cb8b59b00fa545c608/uncompyle6-3.2.5-py35-none-any.whl (209kB)
100% |████████████████████████████████| 215kB 874kB/s
Collecting spark-parser<1.9.0,>=1.8.7 (from uncompyle6)
Downloading https://files.pythonhosted.org/packages/06/13/4da9bccbef8da3c8ff6f113f69992ba34cdbe4d9fb768e25a79f8b0e304b/spark_parser-1.8.7-py3-none-any.whl
Collecting xdis<3.9.0,>=3.8.9 (from uncompyle6)
Downloading https://files.pythonhosted.org/packages/aa/42/beaf8d5bba0589dbb09af7d27ef67d579f24c0da840cc7ef6c482ba3966f/xdis-3.8.9-py35-none-any.whl (87kB)
100% |████████████████████████████████| 92kB 1.0MB/s
Collecting click (from spark-parser<1.9.0,>=1.8.7->uncompyle6)
Downloading https://files.pythonhosted.org/packages/fa/37/45185cb5abbc30d7257104c434fe0b07e5a195a6847506c074527aa599ec/Click-7.0-py2.py3-none-any.whl (81kB)
100% |████████████████████████████████| 81kB 1.0MB/s
Installing collected packages: click, spark-parser, xdis, uncompyle6
Successfully installed click-7.0 spark-parser-1.8.7 uncompyle6-3.2.5 xdis-3.8.9
PS C:\Users\Linyu>
2.查看uncompyle6命令
PS C:\Users\Linyu> uncompyle6 -h
Usage:
uncompyle6 [OPTIONS]... [ FILE | DIR]...
uncompyle6 [--help | -h | --V | --version]
Examples:
uncompyle6 foo.pyc bar.pyc # decompile foo.pyc, bar.pyc to stdout
uncompyle6 -o . foo.pyc bar.pyc # decompile to ./foo.pyc_dis and ./bar.pyc_dis
uncompyle6 -o /tmp /usr/lib/python1.5 # decompile whole library
Options:
-o <path> output decompiled files to this path:
if multiple input files are decompiled, the common prefix
is stripped from these names and the remainder appended to
<path>
uncompyle6 -o /tmp bla/fasel.pyc bla/foo.pyc
-> /tmp/fasel.pyc_dis, /tmp/foo.pyc_dis
uncompyle6 -o /tmp bla/fasel.pyc bar/foo.pyc
-> /tmp/bla/fasel.pyc_dis, /tmp/bar/foo.pyc_dis
uncompyle6 -o /tmp /usr/lib/python1.5
-> /tmp/smtplib.pyc_dis ... /tmp/lib-tk/FixTk.pyc_dis
-c <file> attempts a disassembly after compiling <file>
-d print timestamps
-p <integer> use <integer> number of processes
-r recurse directories looking for .pyc and .pyo files
--fragments use fragments deparser
--verify compare generated source with input byte-code
--verify-run compile generated source, run it and check exit code
--weak-verify compile generated source
--linemaps generated line number correspondencies between byte-code
and generated source output
--help show this message
Debugging Options:
--asm -a include byte-code (disables --verify)
--grammar -g show matching grammar
--tree -t include syntax tree (disables --verify)
--tree++ add template rules to --tree when possible
Extensions of generated files:
'.pyc_dis' '.pyo_dis' successfully decompiled (and verified if --verify)
+ '_unverified' successfully decompile but --verify failed
+ '_failed' decompile failed (contact author for enhancement)
PS C:\Users\Linyu>
3.反编译.pyc文件
反编译后的代码里面只包含有效代码部分和编译时的一些文件信息,但被注释的代码统统没有了,以后一定要做好备份,不要再让悲剧重演!!!
PS C:\Users\Linyu> uncompyle6 .\mainapp.cpython-36.pyc
# uncompyle6 version 3.2.5
# Python bytecode 3.6 (3379)
# Decompiled from: Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 17:00:18) [MSC v.1900 64 bit (AMD64)]
# Embedded file name: C:\Apache24\Test_web\www\mainapp.py
# Compiled at: 2018-12-24 17:48:05
# Size of source mod 2**32: 2280 bytes
import sys
sys.path.insert(1, 'C:\\Apache24\\Test_web\\www')
from flask import Flask, request, render_template
from search import mobile_search, pandas_search
app = Flask(__name__)
@app.route('/')
def index():
return render_template('home.html')
@app.route('/search', methods=['GET'])
def search_form():
return render_template('form.html')
@app.route('/search', methods=['POST'])
def search():
phoneNumber = request.form['phoneNumber']
recordResult = []
destColumn = []
if phoneNumber:
recordResult, destColumn = pandas_search(phoneNumber)
if recordResult:
return render_template('searchresult.html', initial=recordResult[destColumn[0]],
position=recordResult[destColumn[1]],
mobile=recordResult[destColumn[3]],
manager=recordResult[destColumn[2]])
return render_template('form.html', message='No user is found out!')
@app.route('/searchnumber')
def search_number():
phoneNumber = request.args.get('phoneNumber', '')
recordResult = []
destColumn = []
if phoneNumber:
recordResult, destColumn = pandas_search(phoneNumber)
if recordResult:
return render_template('searchresult.html', initial=recordResult[destColumn[0]],
position=recordResult[destColumn[1]],
mobile=recordResult[destColumn[3]],
manager=recordResult[destColumn[2]])
return render_template('form.html', message='No user is found out!')
if __name__ == '__main__':
app.run()
# okay decompiling .\mainapp.cpython-36.pyc
PS C:\Users\Linyu>
如反编译后直接以.py的文件保存到相同的目录下,使用-o .参数
PS C:\Users\Linyu> uncompyle6 -o . mainapp.cpython-36.pyc
# Successfully decompiled file
PS C:\Users\Linyu>
.py的文件自动生成了。