How can I run code that my Python program stored in a string?

46 views Asked by At

So, im trying to make a script that takes code from a pastebin post and runs it. But, for some reason it doesnt run the code. I dont know why. Could someone explain why this wont work so i can fix the issue?

I tried: (dont mind the imports im gonna use those for later)

import os 
from json import loads, dumps
from base64 import b64decode
from urllib.request import Request, urlopen
from subprocess import Popen, PIPE

def get_code():
  test = 'None'
  try:
    test = urlopen(Request('https://pastebin.com/raw/4dnZntN3')).read().decode()
  except:
    pass
  return test

test = get_code()

def main():
  test

main()

The output is empty, and no errors.

2

There are 2 answers

2
Catto eating Python On

you are printing nothing and 'return test' wont be ran because it is outside of the try block

0
vineet singh On

In your main function instead of just printing test use exec(test)

def main():
    exec(test)