Cython編譯python為so 代碼加密示例
1. 編譯出來(lái)的so比網(wǎng)上流傳的其他方法小很多。
2. language_level 是python的主版本號(hào),如果python版本是2.x,目前的版本Cython需要人工指定language_level.
3. python setup.py build_ext --inplace 執(zhí)行腳本
4. 以下是代碼片段
from distutils.core import Extension, setup
from Cython.Build import cythonize
from Cython.Compiler import Options
# __file__ 含有魔術(shù)變量的應(yīng)當(dāng)排除,Cython雖有個(gè)編譯參數(shù),但只能設(shè)置靜態(tài)。
exclude_so = ['__init__.py', "mixins.py"]
sources = ['core', 'libs']
extensions = []
for source in sources:
for dirpath, foldernames, filenames in os.walk(source):
for filename in filter(lambda x: re.match(r'.*[.]py$', x), filenames):
file_path = os.path.join(dirpath, filename)
if filename not in exclude_so:
extensions.append(
Extension(file_path[:-3].replace('/', '.'), [file_path], extra_compile_args = ["-Os", "-g0"],
extra_link_args = ["-Wl,--strip-all"]))
Options.docstrings = False
compiler_directives = {'optimize.unpack_method_calls': False}
setup(
# cythonize的exclude全路徑匹配,不靈活,不如在上一步排除。
ext_modules = cythonize(extensions, exclude = None, nthreads = 20, quiet = True, build_dir = './build',
language_level = 2 或者3 , compiler_directives = compiler_directives))
以上這篇Cython編譯python為so 代碼加密示例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
使用python os模塊復(fù)制文件到指定文件夾的方法
今天小編就為大家分享一篇使用python os模塊復(fù)制文件到指定文件夾的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-08-08
python利用hook技術(shù)破解https的實(shí)例代碼
python利用hook技術(shù)破解https的實(shí)例代碼,需要的朋友可以參考一下2013-03-03
Python?matplotlib數(shù)據(jù)可視化圖繪制
這篇文章主要介紹了Python?matplotlib數(shù)據(jù)可視化圖繪制,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的朋友可以參考一下2022-07-07
python使用Tkinter顯示網(wǎng)絡(luò)圖片的方法
這篇文章主要介紹了python使用Tkinter顯示網(wǎng)絡(luò)圖片的方法,涉及Python操作圖片的相關(guān)技巧,需要的朋友可以參考下2015-04-04
python實(shí)現(xiàn)校園網(wǎng)自動(dòng)登錄的示例講解
下面小編就為大家分享一篇python實(shí)現(xiàn)校園網(wǎng)自動(dòng)登錄的示例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-04-04
Pycharm創(chuàng)建python文件自動(dòng)添加日期作者等信息(步驟詳解)
這篇文章主要介紹了Pycharm創(chuàng)建python文件自動(dòng)添加日期作者等信息(步驟詳解),本文分步驟給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-02-02

