Dynamically load Python application code from a database in Google App Engine

I need to save python code in a database and load it into some bootstrap.py application to execute. I cannot use the file system because I use GAE, so this is my only choice.

However, I am not an experienced python user.

I was already able to download 1 line of code and run it using eval, however, a piece of code with two lines or more gave me an "invalid syntax" error.

I also think that you can extend the "import" loader to implement database loading.

Thanks!

+7
python google-app-engine google-cloud-datastore
source share
4 answers

I was able to do what I intend after having read more about loading dynamic Python code.

Here is a sample code. I removed the headers to make them easier:

Thanks anyway!

==============

class DynCode(db.Model): name = db.StringProperty() code = db.TextProperty(default=None) 

==============

 class MainHandler(webapp.RequestHandler): def get(self): dyn = DynCode() dyn = "index" dyn.code = """ from google.appengine.ext import webapp class MainHandler(webapp.RequestHandler): def get(self): self.response.out.write("Hello World\\n") self.response.out.write("Hello World 2\\n") """ dyn.put() self.response.out.write("OK.") def main(): application = webapp.WSGIApplication([('/update', MainHandler)], debug=True) util.run_wsgi_app(application) if __name__ == '__main__': main() 

====================================

 def main(): query = DynCode.all() dyncodes = query.fetch(1) module = imp.new_module('mymodule') for dyn in dyncodes: exec dyn.code in module.__dict__ application = webapp.WSGIApplication([('/', module.MainHandler)], debug=True) util.run_wsgi_app(application) if __name__ == '__main__': main() 

=========================

+4
source share

I somewhat agree with the commentators above, it sounds dangerous. But:

I experimented a bit with the App Engine console ( http://con.appspot.com/console/ ) and eval() really tended to throw SyntaxError 's.

Instead, the exec statement may be your friend ( http://docs.python.org/release/2.5.2/ref/exec.html ).

I managed to run this in the App Engine console:

 >>> exec "def f(x):\nx = x + 1\ny = 10\n return x + y" >>> f(10) 21 

So try the exec statement, but keep in mind the many, many (many!) Dangers of code coming directly from end users.

+2
source share

If you need a more robust mechanism, you probably want to read the PEP302 , which describes the input hooks. You can use them to import code, not to evaluate it.

+2
source share

I was able to do what I intend after having read more about loading dynamic Python code.

Unfortunately, it’s too bad that it’s broken by design because you don’t understand the operating environment:

App Engine uses several web servers to run your application and automatically adjusts the number of servers used to process requests reliably. This request can be redirected to any server, and it may not be the same server that processed the previous request from the same user.

-5
source share

All Articles