Reputation: 4516
How can I create a file into the temporary files from a encoded string and then execute it?
Upvotes: 1
Views: 3837
Reputation: 12981
Something like this:
import tempfile
import subprocess
with tempfile.TemporaryFile(suffix='.exe', delete=False) as tmp:
tmp.write(EMBEDDED_EXECUTABLE.decode('base64'))
tmp.close()
subprocess.Popen(tmp.name)
Upvotes: 3
Reputation: 32908
You have the Windows tag, so I'll answer accordingly.
You will need to, among other valid possibilities, prefix an underscore before lowercase characters, and replace slashes with hyphens, in order to make the Base64 sequence a valid filename as well as unique.
Upvotes: 0
Reputation: 72029
You'll probably be interested in the tempfile
module, as well as base64
and execfile
.
BTW, you may want to provide some more detail about what you're trying to accomplish. This sounds like it has a good chance of being either a bad idea, unnecessary work, or both.
Upvotes: 3
Reputation: 38492
decode the string (pydoc base64); make a tempfile (pydoc tempfile), write the string; and use any number of the os functions to execute it (maybe pydoc os.popen).
Upvotes: 1