Python一段用於保密的自動銷燬程式碼
阿新 • • 發佈:2018-11-11
有的程式碼檔案,可能我們放到伺服器上,執行規定的次數如1次後,就不再需要了,或者為了對程式碼進行保密,在伺服器上臨時執行一次,程式執行還未結束或伺服器突然斷電,程式檔案內容即消失。
有兩種方式,可以在執行程式開始,隨著程式碼載入記憶體開始,對該程式碼檔案實行檔案銷燬,或者對程式碼檔案裡內容進行銷燬。
大家可以試試下面的程式碼,嘿嘿~~
print "code start" def self_delete(max_times): import os file_name = os.path.split(__file__)[1] times = 0 try: with open('times', 'r') as fp: times = int(fp.readlines()[0]) except: pass if times < max_times-1: times += 1 try: with open('times', 'w') as fp: fp.writelines(str(times)) except: pass else: try: os.remove('times') except: pass print "delete codes!!!" ## delete file!!! # os.remove(file_name) ## delete codes!!! with open(file_name, 'r') as fp: codes = fp.readlines() with open(file_name, 'w') as fp: fp.writelines(codes[:1]+codes[-1:]) self_delete(1) # set your codes running times #TODO ''' add your codes here ''' print 'add your codes here' import time for i in range(10): print 'hello world' time.sleep(i) print "code end"