1

I am using python 2.4 for a program which imports scripts from the internet and executes them so a script could be changed by the author and the user wouldn't have to re-download the script.

This is the part of the program that downloads the script:

def downloadScript(self,script):
    myfile=open('#A file path/'+script['name']+'.txt','w')
    try:
        downloadedScript=urllib.urlopen(script['location']).read()
    except:
        #raise error
        return
    myfile.write(downloadedScript)
    myfile.close()

def loadScript(self):
    if not self.scriptCurrentlyLoaded:
        script=self.scripts[self.scroller.listPos]
        if script['location']=='None':
            #raise error
            return
        self.downloadScript(script)
        myfile=open('#A file path/'+script['name']+'.txt','r')
        for line in myfile:
            if line.startswith('from') or line.startswith('import'):
                exec(line.strip()) #This was added because of the name errors
                                   #being produced but to no affect
        myfile.close()
        execfile('#A file path/'+script['name']+'.txt')
        self.scriptCurrentlyLoaded=True
        self.scriptLoaded=script
    else:
        #raise error

The very odd thing is that when I run

execfile(script path)

outside of the function, after the script has been downloaded, the script is executed correctly. But trying to run the loadScript function raises name errors in the script even though the names have been imported in the script and before the execfile which I find very odd.

So my question is: Am I using a very bad method to download and execute these scripts?

Sorry if this question was answered before but I can't seem to find anyone else who is trying to run python scripts by downloading them from the internet.

Edit: adding globals as another argument to the execfile has seemed to fix the problem for now. I don't know if any other problems will occur later though.

Freshollie
  • 171
  • 6
  • 13
  • Just a quick note, in downloadScript, you potentially open the file without closing it in the case of `urlopen` raising an exception. –  Jul 04 '13 at 11:12
  • you know, it's not mandatory to save the downloaded script into a file : you can use eval to evaluate a string containing a code. Also, I hope what are you doing is not critical, since it is very dangerous to execute arbitrary code from external source without checking it first. – lucasg Jul 04 '13 at 11:13
  • @BrandonInvergo In this case I would fix those problems when I come to them but at the moment the importing of scripts is the main function of the program so without it the program would be useless, but yes I will fix that. – Freshollie Jul 04 '13 at 11:30
  • @georgesl When running eval(downloadScript) File "", line 1 import random ^ SyntaxError: invalid syntax. Also its up to the users of the program whether they are going to add scripts made by authors they do not trust. – Freshollie Jul 04 '13 at 11:34
  • 1
    Perhaps you want to change exec(line.strip()) to exec(line.strip(), globals) so that the imports are attached to the global namespace. Similarly, probably want to change execfile('#A file path/'+script['name']+'.txt') to execfile('#A file path/'+script['name']+'.txt', globals()) to match. – dilbert Jul 04 '13 at 12:46
  • @dilbert Thanks, that seems to have fixed the problem. I got rid of the `exec(line.strip())` because that was only there because of the importing issues in the script but adding globals to the arguments of the execfile seemed to fix it. I will check back if I find any other problems though. – Freshollie Jul 04 '13 at 13:09

1 Answers1

3

In R you can simply 'source(url)'. Here's the closest I have found so far in python:

import urllib
(fn,hd) = urllib.urlretrieve('http://host.com/file.py')
execfile(fn)
  • 2
    If you take a look at the question, you will see that OP already had this solution (+ some overhead) and is having a specific issue with that. Your answer does not address that issue. – Christoph Sep 03 '15 at 15:42