- 将python程序发布出去,为了安全性,尽量的不适用源码,而是使用转换后的代码。
- 在将python转换这里说明一下compile的使用方式,自己编写代码(有一部分是从其他地方抄来的,经过自己修改,并证实可以使用);当然这个pyc好像可以使用其他工具反编译,但是这个还是有一定的安全性的
- 编写pyc的代码
-
这个文件自己拷贝到一个py文件中,取名pyc_create.py,名字在后边要用的 import os import sys import shutil from py_compile import compile #print "argvs:",sys.argv if len(sys.argv) == 3: comd = sys.argv[1] #输入的命令 path = sys.argv[2] #文件的地址 if os.path.exists(path) and os.path.isdir(path): for parent,dirname,filename in os.walk(path): for cfile in filename: fullname = os.path.join(parent,cfile) if comd == 'clean' and cfile[-4:] == '.pyc': try: os.remove(fullname) print("Success remove file:%s" % fullname) except: print("Can't remove file:%s" % fullname) if comd == 'compile' and cfile[-3:] == '.py': #在这里将找到的py文件进行编译成pyc,但是会指定到一个叫做__pycache__的文件夹中 try: compile(fullname) print("Success compile file:%s" % fullname) except: print("Can't compile file:%s" % fullname) if comd == 'remove' and cfile[-3:] == '.py' and cfile != 'settings.py' and cfile != 'wsgi.py': try: os.remove(fullname) print("Success remove file:%s" % fullname) except: print("Can't remove file:%s" % fullname) if comd=='copy' and cfile[-4:] == '.pyc': parent_list = parent.split("\\")[:-1] parent_up_path = '' for i in range(len(parent_list)): parent_up_path+=parent_list[i]+'\\' shutil.copy(fullname,parent_up_path) print('update the dir of file successfully') if comd=='cpython' and cfile[-4:] =='.pyc': cfile_name = '' cfile_list = cfile.split('.') for i in range(len(cfile_list)): if cfile_list[i]=='cpython-35': continue cfile_name+=cfile_list[i] if i==len(cfile_list)-1: continue cfile_name+='.' shutil.move(fullname,os.path.join(parent,cfile_name)) print('update the name of the file successfully') else: print("Not an directory or Direcotry doesn't exist!") else: print("Usage:") print("\tpython compile_pyc.py clean PATH\t\t#To clean all pyc files") print("\tpython compile_pyc.py compile PATH\t\t#To generate pyc files") print("\tpython compile_pyc.py remove PATH\t\t#To remove py files")
- 使用的方法是:
- 先将文件编译成pyc文件:在cmd中,指定到pyc_create.py的路径下:python pyc_create compile 需要编译的文件的根路径;这样就可以将要编译的文件编译成pyc文件了,但是文件都存在各个文件夹的__pycache__中
- 将__pycache__中的pyc文件拷贝到原来py存在的位置:python pyc_create copy 文件的根路径;这样就将编译的文件存在原来py文件的位置了
- 将原来的py文件删掉就可以了:python pyc_create remove 文件根路径
- 将文件的名字进行更改,我这里使用的是pyhon 3.5版本的,所以文件的名字变成了*.cpython-35.pyc,要将其中的cpython-35去掉,不然不能正常运行,因为文件的名字默认最后一个后缀是扩展名,其他的为文件名字
- 执行完毕,就可以使用了。
python django 使用pyc形式发布
最新推荐文章于 2022-10-31 16:30:04 发布