Calling python from python - persistence of module imports?

So I have some Python scripts, and I've got a BaseHTTPServer to serve up their responses. If the requested file is a .py then I'll run that script using execfile(script.py).

The question is this: are there any special rules about imports? One script needs to run just once, and it would be good to keep the objects it creates alive between requests. Can I trust that that will happen?

Does script run via execfile() run any differently, or have any scope access issues?


Asked by: Dominik476 | Posted: 06-12-2021






Answer 1

The documentation for the execfile method is here. Since no particular version of python was specified, I'm going to assume we're talking about 2.6.2.

The documentation for execfile specifies it takes three arguments: the filename, a dictionary (to act as the local variables), and a second dictionary (to act as the global variables). If you omit the second and third arguments, the file's contents are run in their own scope (like a module) that captures local variables but exposes global variables to the parent scope. So if the file creates local variables, they won't be retained, but global variables will be retained.

However, running execfile without local and global contexts specified means that the file sees the locals and globals of the calling function. For code that you don't trust, this should be considered a security hole. It may generally be wise to create two dictionaries for locals and globals and pass those in as the second and third arguments to execfile. If you keep those dictionaries somewhere (such as in another dictionary keyed by the filename), then you could re-use those dictionaries the next time the file is served, which will keep the objects created by the file alive.

So in short: execfile isn't exactly like import. But you can retain dictionaries of the locals and globals to keep the results of an execfile call around to be used again.

Answered by: Chester436 | Posted: 07-01-2022



Answer 2

I recommend not using execfile. Instead, you can dynamically import the python file they request as a module using the builtin __import__ function. Here's a complete, working example that I just wrote and tested:

from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer

class Handler(BaseHTTPRequestHandler):
    def do_GET(self):
        self.send_response(200)
        self.send_header("Content-type", "text/plain")
        self.end_headers()

        filename = self.path.lstrip("/")
        self.wfile.write("You requested " + filename + "\n\n")
        if filename.endswith(".py"):
            pyname = filename.replace("/", ".")[:-3]
            module = __import__(pyname)
            self.wfile.write( module.do_work() )

HTTPServer(("",8080), Handler).serve_forever()

So in this case, if someone visits http://localhost:8080/some_page then "You requested some_page" will be printed.

But if you request http://localhost:8080/some_module.py then the file some_module.py will be imported as a Python module and the do_work function in that module will be called. So if that module contains the code

def do_work():
    return "Hello World!"

and you make that request, then the resulting page will be

You requested some_module.py

Hello World!

This should be a good starting point for dealing with such things. As an aside, if you find yourself wanting a more advanced web server, I highly recommend CherryPy.

Answered by: Blake286 | Posted: 07-01-2022



Similar questions

python - Raising events and object persistence in Django

I have a tricky Django problem which didn't occur to me when I was developing it. My Django application allows a user to sign up and store his login credentials for a sites. The Django application basically allows the user to search this other site (by scraping content off it) and returns the result to the user. For each query, it does a couple of queries of the other site. This seemed to work fine but sometimes, the other...


python - Cairo context and persistence?

I am just getting started using pycairo, and I ran into the following interesting error. The program I write creates a simple gtk window, draws a rectangle on it, and then has a callback to draw a random line on any kind of keyboard input. However, it seems that with each keyboard input, I have to create a new context, or I get an error at the moment the program receives first keyboard input (specifically, on the .stroke()...


Data persistence for python when a lot of lookups but few writes?

I am working on a project that basically monitors a set remote directories (FTP, networked paths, and another), if the file is considered new and meets criteria we download it and process it. However i am stuck on what the best way is to keep track of the files we already downloaded. I don't want to download any duplicate files, so i need to keep track of what is already downloaded. Orignally i was storing it as a ...


python - zeromq persistence patterns

Who has to manages the persistent in the ZeroMQ? When we use the ZeroMQ clients in Python language, what are the plug-ins/modules available to manage the persistent? I would like to know the patterns to use the ZeroMQ.


persistence - Atomic state storage in Python?

I'm working on a project on an unreliable system which I'm assuming can fail at any point. What I want to guarantee is that if I write_state and the machine fails mid-operation, a read_state will either read a valid state or no state at all. I've implemented something which I think will work below -- I'm interested in criticism of that or alternative solutions if anyone knows of one. My idea: import...


process - Python object persistence over re-execs

Is where a way to persist objects over re-execs of a running script? If I want a running script to re execute itself to pick up any code changes, (os.exec*) is there a way to persist the objects for access after the re-execution? I could set environment variables with pickled ascii data, or write that data to a pipe and re-read it after the re-execution, but that seems inelegant or like a hack. Even if doing that, not a...


persistence - Python: how to save a Binary Tree?

I was wondering how to save a Binary Tree that I have previously created. Does anyone know how to do it? Thank you so much. PD: Here there is a link about how to implement a binary tree, I am using this pice od code: http://code.activestate.com/recipes/286239-binary-ordered-tree/


oop - persistence of objects in python

Till now I have done only procedural programming in C so I am rather unclear about both OOP and Python. I have a large project which contains a number of python files. File a.py defines a class called fobject I am using python 2.5 File b.py and c.py have classes called BProject and CProject which have an object of fobject as parameter. I have included using import CProject (defined in c.py) in b.py. I have ...


persistence - Storing data on the fly with Python

I have a program where I want to record the function name, parameters passed to the function and the result. I am currently recording this using a decorator. However I am unsure how to store this. Currently I am just appending to a file each time. I want to just be able to add my decorators to any function in any python program and record the result. As such I need to be able to store the data with each time. This ...


python - Computing persistence number of an integer

I am trying to make a code that does the following: Multiplying the digits of an integer and continuing the process gives the surprising result that the sequence of products always arrives at a single-digit number. For example: 715 -> 35 -> 15 -> 5 88 -> 64 -> 24 -> 8 27 -> 14 -> 4 The number of products necessary to ...






Still can't find your answer? Check out these communities...



PySlackers | Full Stack Python | NHS Python | Pythonist Cafe | Hacker Earth | Discord Python



top