0

This function grabs a python script from a paste on pastebin with the title py_0001, when the execution reaches the try: except: it throws an error SyntaxError: unexpected character after line continuation character

If you copy value of script_ and declare it as a string variable it executes without any errors

The function works fine until it reaches the error handling part

def get_script():
    ''' grabs python script from pastebin '''
    reg_ = r'[a-zA-Z0-9]*\">py_0001'
    resp = requests.get(url='https://pastebin.com/u/'+usr_name)
    path = re.findall(reg_ , str(resp.content) , re.MULTILINE)  
    url2 = "https://pastebin.com/raw/"+ str(path[0]).replace('">py_0001' , '')
    resp2 = requests.get(url2)
    script_ = str(resp2.content)[2:-1]
    print (script_)


  
    try:
      exec(script_)
    except:
      print ("3rr0r")

This is the output of the paste on pastebin

import os\r\nimport time \r\nimport random \r\n \r\ndef fun_9991():\r\n    ## a simple code example to test \r\n    for i in range (0 , 10 ):\r\n        print ( " loop count {} , random number is {} , time is {} ".format(i , random.randrange(10) , int(time.time()/1000)))\r\n    print ("loop reached the end")\r\n    \r\n \r\nif __name__ == "__main__":\r\n    fun_9991()\r\n\r\n\r\n
3
  • 2
    what are you trying to exec? is it the complete python script? Commented Jan 18, 2021 at 13:18
  • 2
    You should pretty much never call str() on a bytes object. Use .decode instead. Commented Jan 18, 2021 at 13:28
  • i am trying to execute the last bit of code Commented Jan 18, 2021 at 13:30

1 Answer 1

1

Your problem is calling str() on a bytes object. NEVER call str() on a bytes object to convert it to a string, since it behaves like repr(). Simply using [2:-1] will only remove the quotes but not undo escaping other special characters.

You can do this:

script_ = resp2.content.decode('utf-8')

Or this:

script_ = resp2.text

Also, executing random code from the internet is an incredibly bad idea.

Sign up to request clarification or add additional context in comments.

1 Comment

I am creating this random code and i am using a VM to test it out

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.