import tempfile, subprocess
def py3in2(func_text, arg):
# escape char in arg should be escaped twice.
payload = ['#/usr/bin/env python3', 'def _func(arg):']
for line in func_text.split('\n'):
payload.append(' ' + line)
payload.append('with open("/dev/fd/1", "w") as f:\n f.write(_func(\''+arg+'\'))\n')
payload = '\n'.join(payload)
with tempfile.NamedTemporaryFile() as tf:
tf.file.write(payload)
tf.file.close()
output = subprocess.check_output(["python3", tf.name], stderr=subprocess.PIPE)
return output
ret = py3in2('''
import sys
return sys.version
''', '')
print ret
Leave a Reply