Skip to content

Instantly share code, notes, and snippets.

@raeq
Created July 19, 2020 12:30
Show Gist options
  • Save raeq/2907fb033de5c35f4fb140400345dc72 to your computer and use it in GitHub Desktop.
Save raeq/2907fb033de5c35f4fb140400345dc72 to your computer and use it in GitHub Desktop.
Execute code found in strings
import subprocess
import ast
def exec_string(input_string: str = "") -> str:
result = subprocess.getoutput(input_string)
return result
def eval_string(input_string: str = "") -> str:
result = ast.literal_eval(input_string)
return str(result)
assert exec_string("ls -l")
assert eval_string("{ 'key':'value' }") == "{'key': 'value'}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment