服務器環境python項目加密

1、應用場景

  Python是一種面向對象的解釋型計算機程序設計語言,具有豐富和強大的庫,使用其開發產品快速高效。

  python的解釋特性是將py編譯爲獨有的二進制編碼pyc文件,然後對pyc中的指令進行解釋執行,但是pyc的反編譯卻非常簡單,可直接反編譯爲源碼,當需要將產品發佈到外部環境的時候,源碼的保護尤爲重要.

    準備工作

  環境是可爲linux/centos,我Windows10本地是Bash on Ubuntu on Windows,用起來很方便,命令行打bash即進入命令行

  思路是先將py轉換爲c代碼,然後編譯c爲so文件

  所以要安裝以下內容

    python 安裝:cython

      pip install cython

    linux 安裝:python-devel,gcc

      yum install python-devel

      yum install gcc

 

2、服務器環境如何給python項目加密

直接上代碼:將如下代碼放入需要加密的文件夾,即可

參考:https://www.cnblogs.com/ke10/p/py2so.html

#-* -coding: UTF-8 -* -
__author__ = 'Arvin'

import sys, os, shutil, time
from distutils.core import setup
from Cython.Build import cythonize

starttime = time.time()
currdir = os.path.abspath('.')
parentpath = sys.argv[1] if len(sys.argv)>1 else ""
setupfile= os.path.join(os.path.abspath('.'), __file__)
build_dir = "build"
build_tmp_dir = build_dir + "/temp"

def getpy(basepath=os.path.abspath('.'), parentpath='', name='', excepts=(), copyOther=False,delC=False):
    """
    獲取py文件的路徑
    :param basepath: 根路徑
    :param parentpath: 父路徑
    :param name: 文件/夾
    :param excepts: 排除文件
    :param copy: 是否copy其他文件
    :return: py文件的迭代器
    """
    fullpath = os.path.join(basepath, parentpath, name)
    for fname in os.listdir(fullpath):
        ffile = os.path.join(fullpath, fname)
        #print basepath, parentpath, name,file
        if os.path.isdir(ffile) and fname != build_dir and not fname.startswith('.'):
            for f in getpy(basepath, os.path.join(parentpath, name), fname, excepts, copyOther, delC):
                yield f
        elif os.path.isfile(ffile):
            ext = os.path.splitext(fname)[1]
            if ext == ".c":
                if delC and os.stat(ffile).st_mtime > starttime:
                    os.remove(ffile)
            elif ffile not in excepts and os.path.splitext(fname)[1] not in('.pyc', '.pyx'):
                if os.path.splitext(fname)[1] in('.py', '.pyx') and not fname.startswith('__'):
                    yield os.path.join(parentpath, name, fname)
                elif copyOther:
                        dstdir = os.path.join(basepath, build_dir, parentpath, name)
                        if not os.path.isdir(dstdir): os.makedirs(dstdir)
                        shutil.copyfile(ffile, os.path.join(dstdir, fname))
        else:
            pass

#獲取py列表
module_list = list(getpy(basepath=currdir,parentpath=parentpath, excepts=(setupfile)))
try:
    setup(ext_modules = cythonize(module_list),script_args=["build_ext", "-b", build_dir, "-t", build_tmp_dir])
except Exception as ex:
    print("error! ", ex.message)
else:
    module_list = list(getpy(basepath=currdir, parentpath=parentpath, excepts=(setupfile), copyOther=True))

module_list = list(getpy(basepath=currdir, parentpath=parentpath, excepts=(setupfile), delC=True))
if os.path.exists(build_tmp_dir): shutil.rmtree(build_tmp_dir)

print("complate! time:", time.time()-starttime, 's')

2、然後生成的build文件夾就是編譯後的文件

如果有入口文件,那麼將入口文件拷貝到指定build下的文件夾

3、啓動服務

運行入口文件

 

二、新代碼

https://github.com/striver-ing/encrypt-py

https://github.com/striver-ing/encrypt-py

發佈了78 篇原創文章 · 獲贊 142 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章