Embed pickle (or arbitrary) data in python script

前端 未结 4 1019
孤城傲影
孤城傲影 2021-01-13 10:08

In Perl, the interpreter kind of stops when it encounters a line with

__END__

in it. This is often used to embed arbitrary data at the end

4条回答
  •  一生所求
    2021-01-13 10:35

    I made this code. You run something like python comp.py foofile.tar.gz, and it creates decomp.py, with foofile.tar.gz's contents embedded in it. I don't think this is really portable with windows because of the Popen though.

    import base64
    import sys
    import subprocess
    
    inf = open(sys.argv[1],"r+b").read()
    outs = base64.b64encode(inf)
    
    decomppy = '''#!/usr/bin/python
    import base64
    
    def decomp(data):
        fname = "%s"
        outf = open(fname,"w+b")
        outf.write(base64.b64decode(data))
        outf.close()
    
        # You can put the rest of your code here.
        #Like this, to unzip an archive
        #import subprocess
        #subprocess.Popen("tar xzf " + fname, shell=True)
        #subprocess.Popen("rm " + fname, shell=True)
    ''' %(sys.argv[1])
    
    taildata = '''uudata = """%s"""
    decomp(uudata)
    ''' %(outs)
    
    outpy = open("decomp.py","w+b")
    outpy.write(decomppy)
    outpy.write(taildata)
    outpy.close()
    subprocess.Popen("chmod +x decomp.py",shell=True)
    

提交回复
热议问题