1. 程式人生 > >將python源程式編譯為pyc或pyo位元組碼程式

將python源程式編譯為pyc或pyo位元組碼程式

為了提高自己搭建的Django站點的安全性,將加密後的資料庫連線資訊以及加密使用的資訊單獨存放在了一個配置檔案中,之後寫了一箇中間層來解密配置檔案中的密文並用於站點應用連線資料庫。雖然這樣一種安全方式很初級,但起碼比明文存放密碼等敏感資訊要好的多。

現在面臨另外一個問題,因為最終密文形式的密碼還是需要解密後才能使用的,上邊提到的用於解密配置檔案中的密文的中間層正是用於此目的。若惡意使用者獲取到了相關檔案,很容易根據檔案中的程式碼推測出加密使用的演算法,也很容易獲取的明文的密碼,因此現在需要一種方式將python原始碼轉換成不能被“小人”輕易讀取的檔案形式。

怎麼轉換呢?其實通過將python源程式.py檔案編譯為位元組碼檔案.pyc或.pyo就可以很好的達到上述目的(儘管這仍有可能被惡意著反編譯為.py原始檔,但至少算是有加強了一層防護,是無意的“君子”不至於這麼輕鬆獲取密碼。)

那麼該如何編譯python源程式呢?

可以有多重形式:
python -m py_compile file.py #把單個.py檔案編譯為位元組碼檔案
python -m py_compile /path/to/src/ #批量生成位元組碼檔案,/path/to/src/是包含.py檔名的路徑
python -m compileall file.py #把單個.py檔案編譯為位元組碼檔案
python -m compileall /path/to/src/ #批量生成位元組碼檔案,/path/to/src/是包含.py檔名的路徑

或者:
python -O -m py_compile file.py
python -O -m py_compile /path/to/src/
python -O -m compileall file.py
python -O -m compileall /path/to/src/

或者
python -OO -m py_compile file.py
python -OO -m py_compile /path/to/src/
python -OO -m compileall file.py
python -OO -m compileall /path/to/src/

或者通過指令碼來編譯:
import py_compile
py_compile.compile('file.py')

import compileall
compileall.compile_file('file.py')

import compileall
compileall.compile_dir('/path/to/src/')
compileall.compile_path()

具體引數:
    compile(file, cfile=None, dfile=None, doraise=False)
        Byte-compile one Python source file to Python bytecode.
        
        Arguments:
        
        file:    source filename
        cfile:   target filename; defaults to source with 'c' or 'o' appended
                 ('c' normally, 'o' in optimizing mode, giving .pyc or .pyo)
        dfile:   purported filename; defaults to source (this is the filename
                 that will show up in error messages)
        doraise: flag indicating whether or not an exception should be
                 raised when a compile error is found. If an exception
                 occurs and this flag is set to False, a string
                 indicating the nature of the exception will be printed,
                 and the function will return to the caller. If an
                 exception occurs and this flag is set to True, a
                 PyCompileError exception will be raised.


    compile_dir(dir, maxlevels=10, ddir=None, force=0, rx=None, quiet=0)
        Byte-compile all modules in the given directory tree.
        
        Arguments (only dir is required):
        
        dir:       the directory to byte-compile
        maxlevels: maximum recursion level (default 10)
        ddir:      the directory that will be prepended to the path to the
                   file as it is compiled into each byte-code file.
        force:     if 1, force compilation, even if timestamps are up-to-date
        quiet:     if 1, be quiet during compilation
    
    compile_file(fullname, ddir=None, force=0, rx=None, quiet=0)
        Byte-compile one file.
        
        Arguments (only fullname is required):
        
        fullname:  the file to byte-compile
        ddir:      if given, the directory name compiled in to the
                   byte-code file.
        force:     if 1, force compilation, even if timestamps are up-to-date
        quiet:     if 1, be quiet during compilation
    
    compile_path(skip_curdir=1, maxlevels=0, force=0, quiet=0)
        Byte-compile all module on sys.path.
        
        Arguments (all optional):
        
        skip_curdir: if true, skip current directory (default true)
        maxlevels:   max recursion level (default 0)
        force: as for compile_dir() (default 0)
        quiet: as for compile_dir() (default 0)

註解:
-m引數相當於指令碼中的import,這裡的-m py_compile 相當於上面的 import py_compile,也即把後邊跟隨的庫模組當做指令碼執行。這樣生成的位元組碼檔案字尾名為.pyc
-O引數表明要生成更加緊湊的優化後的位元組碼,-OO會進一步移除-O選項生成的優化後的位元組碼檔案中的文件字串。這樣生成的位元組碼檔案字尾名為.pyo,對於.pyo檔案可以通過 python命令加-O引數執行匯入了該模組的python程式來呼叫。
需注意的是,不同版本編譯後的pyc檔案是不同的,比如2.5編譯的pyc檔案2.4版本的python是無法執行的。










相關推薦

python源程式編譯pycpyo位元組程式

為了提高自己搭建的Django站點的安全性,將加密後的資料庫連線資訊以及加密使用的資訊單獨存放在了一個配置檔案中,之後寫了一箇中間層來解密配置檔案中的密文並用於站點應用連線資料庫。雖然這樣一種安全方式很初級,但起碼比明文存放密碼等敏感資訊要好的多。 現在面臨另外一個問題,因

pythonpy檔案轉換pyc

什麼是pyc檔案 pyc是一種二進位制檔案,是由py檔案經過編譯後,生成的檔案,是一種byte code,py檔案變成pyc檔案後,載入的速度有所提高,而且pyc是一種跨平臺的位元組碼,是由python的虛擬機器來執行的,這個是類似於JAVA或者.NET的虛擬機器的概念。pyc的內容,是跟python的版本

python檔案打包-編譯pyc

什麼是pyc檔案pyc是一種二進位制檔案,是由py檔案經過編譯後,生成的檔案,是一種byte code,py檔案變成pyc檔案後,載入的速度有所提高,而且pyc是一種跨平臺的位元組碼,是由python的虛擬機器來執行的,這個是類似於JAVA或者.NET的虛擬機器的概念。pyc

python py指令碼批量編譯pyc

什麼是pyc檔案? pyc是一種二進位制檔案,是由py檔案經過編譯後,生成的檔案, 是一種byte code,py檔案變成pyc檔案後, 載入的速度有所提高,而且pyc是一種跨平臺的位元組碼,

編譯pythonpyc/pyo位元組檔案

github專案:點選開啟連結 執行環境需要python2.7,可以反編譯2.5-2.7的python位元組碼。 下載專案後執行python setup.py install即可(執行該命令輸出的內容

Python 判斷是否質數素數

span mil 根據 inpu 自然數 執行 一個數 input round 一個大於1的自然數,除了1和它本身外,不能被其他自然數(質數)整除(2, 3, 5, 7等),換句話說就是該數除了1和它本身以外不再有其他的因數。 首先我們來第一個傳統的判斷思路: def ha

CentOS 7 python版本升級3.x後產生的各種問題

eba traceback roo python版本 腳本 3.x fire pos rec CentOS 7內置的 Python版本為2.x,很多程序依賴於2.x版本,修改後會導致部分python腳本失效。 例如:yum、firewall-cmd、firewall-co

阿里的架構師Python基礎總結千行程式碼,只讓更多的人學好Python

某天大佬很是自信的告訴我,只要學會這千行程式碼,不管你是零基礎還是弱基礎或是沒有接觸過程式設計,都可以快速入門Python!當時我就不信邪啊,等我看完之後,即使作為一個Python老鳥了,還是領會到了很多大佬的獨特見解!   麻省理工教授將Python基礎總結成千行程式碼,讓

Python檔案格式 .py .pyc .pyw .pyo .pyd的主要區別

Python是一種面向物件、解釋型計算機程式設計語言。Python 語法簡潔、清晰,具有豐富和強大的類庫。Python原始碼遵循 GPL (GNU General Public License) 協議,由 Guido van Rossum 於 1989 年底發明,第一個公開發行版發行於 1991 年。Pyth

【pycharm】 如何python檔案打包exe格式

首先我是通過Pyinstall打包的,具體安裝及打包步驟如下 1.開啟終端控制檯  通過pip命令進行安裝   pip install  PyInstall 2.接著會自動下載,安裝成功後 通過Pyinstall自帶命令進行打包 3.控制檯輸入  Pyinstall

如何war反編譯java專案

War包反編譯過程 很多人可以將專案編譯為war釋出,可是有時候得到war確看不到原始碼。今天分享下war反編譯的過程: 1.首先下載一個小工具,在http://jd.benow.ca/官網下載jd-gui工具,按照自己的系統下載。 2.將自己的war字尾改為.zip,

pycharm如何python檔案打包exe格式

因為近期正在學習python,就需要將python檔案打包為exe可執行檔案,就將該過程記錄下來。 首先我是通過Pyinstall打包的,具體安裝及打包步驟如下 1.開啟終端控制檯  通過pip命令進行安裝   pip install  PyInstall 2.接著

linux下Python指令碼打包可執行檔案

一. 下載pyinstaller 連結 二. 解壓 無需安裝,解壓即可使用 三. 輸入命令,進行打包 命令格式: pyinstaller_path/pyinstaller.py -F s

python指令碼轉化可執行檔案exe

1、安裝pywin32 2、安裝pyinstaller 3、將需要轉換的.py指令碼拷貝至pyinstaller安裝資料夾下 4、在cmd控制檯下進入安裝目錄下,並且執行如下命令 python

python的py、pycpyo、pyd檔案區別

1.如果需要特殊的單獨編譯,則只需要使用py_complie這個模組就行了,如下import py_compilepy_compile.compile(r‘H:\game\test.py‘)compile函式原型:compile(file[, cfile[, dfile[, doraise]]])file 表

py檔案編譯pyc(命令與指令碼)

http://www.cnblogs.com/dkblog/archive/2009/04/16/1980757.html 其實很簡單, 用 python -m py_compile file.py python -m py_compile /root/src/{f

C#中使用OpenGL:(三).lib檔案編譯.dll檔案

C#不能呼叫C/C++lib檔案的函式,但能呼叫dll檔案的函式。可是現在的情況是,我只有一個lib檔案,函式的實現都在裡邊了。能不能把lib變為dll呢?答案是肯定的。 要把lib編譯為dll大概有三種方法吧: 第一種,就是把lib的函式封裝一下,然後用編

(JSP)如何網頁資料輸出ExcelWord檔案

將一個jsp頁面中的<%@ page contentType="text/html; charset=GBK" language="java" %>替換為 <%@ page contentType="application/vnd.ms-Excel;ch

編譯一個java源程式檔案,會產生多少個位元組檔案

一般情況下一個Java檔案代表一個類,在編譯時會產生一個位元組碼.class檔案。 但是在Java中 一個原始檔中可以包含多個類,但是隻能有一個public類,其他的都成為內部類,這時編譯時

Shiny APP搭建獨立的桌面可執行程式 - Deploying R shiny app as a standalone application

[toc] ## 起源! 某天,我發現了Shiny這個東西,當時興沖沖的嘗試官網上各種各樣的例子,最後發現這個東西似乎只能充當一個“玩具”。如果要在本地執行,它需要一個完整的R環境,這對相當一部分使用者來說是極度不友好的。另外,Rstudio主張將Shiny部署在https://www.shinyapps