Have Users Change a Running Python Script's Variables over the Web?
May 22, 2013 12:51 PM   Subscribe

I'm writing a simple simulator with python and I would like to be able to have my users log in to the simulator over the internet and change their own set of variables.

I know next-to-nothing on web programming and I'm not to concerned about security, the simulation will be on it's own dedicated machine.
posted by millerizer to Computers & Internet (14 answers total) 2 users marked this as a favorite
 
What's the output of the simulator? Is this just something users set the initial parameters on and it executes, or is this something where users can tweak the parameters as the simulation runs?
posted by RonButNotStupid at 12:59 PM on May 22, 2013


Response by poster: Oh! I should have said that.

It is something that they could tweak the parameters while it is running.
posted by millerizer at 1:01 PM on May 22, 2013


In that case, you really need to collaborate with someone who knows how to do this kind of thing (live web updates, visualization, etc, etc). I don't there's any easy answer.
posted by RonButNotStupid at 1:04 PM on May 22, 2013


Response by poster: Okay thanks, but to clarify, I don't need live web updates or anything like that. The results would stay on the machine running the simulation. All I would need is for my users to be able to change the parameters.
posted by millerizer at 1:15 PM on May 22, 2013


I think Pyro or RPyC would do what you want with the least pain. RPyC sounds a little easier to use, although I haven't used either personally. You would build your Web stuff in a Python framework (say Bottle, if you want something tiny and simple, or Django, if you want something fancy and powerful) and use RPyC directly from there to make changes on your remote machine.
posted by pocams at 1:27 PM on May 22, 2013


I've often thought about this, have a running program update its code during runtime.

In ruby it would be fairly straightforward, I *think* the same approach would work for Python using reload(module).

In essence you have a web interface that updates the source code file of the module.

In the main program, when the source code is updated you fire the reload command.

The running program should then start using the new variables. Note that it's quite easy to crash the program this way.

If stability of the running program is important, then pocams RPyC recommendation can be used to better encapsulate what the user is allowed to modify for better code integrity.

I always thought it'd be interesting to have a group of coders modify a running program in real time using dynamic code. For example, starting with a basic game, have the game play changed during play. Sounds like a fun experiment to me.
posted by forforf at 1:42 PM on May 22, 2013


You need to be a lot more specific.

I would like to be able to have my users:
log on to the simulator over the internet.

What do you mean by "log on?" Are they going to be given a username and a password? Is each user going to have their own username and password?

change their own set of variables

Does this mean that each user is going to have their own instance of the simulation, so that changing their variables only applies to their instance of the simulation? Or is there one instance of the simulation and each user changes a subset of the inputs? Or something else?

I know next-to-nothing on web programming and I'm not to concerned about security, the simulation will be on it's own dedicated machine.

If you hook your simulation to the web, you are doing web programming. If you are doing web programming, you should be concerned about security. Running the simulation on it's own dedicated machine may be part of a security strategy, but it probably isn't sufficient. You'll probably, at the very least, want to sanitize user input to prevent code injection. You'll also want the simulation running as a user with minimal privileges to make other changes to the machine, and you'll probably want the machine firewalled-off from other systems.

Since you are doing web programming, you'll may also need a concurrency strategy for the eventually that two or more users submit inputs at the same time.

It is something that they could tweak the parameters while it is running.

Is the simulation running continuously, or does it just run in response to user inputs? Is the state of the simulation preserved between inputs, or does it start from the same initial state each time a user tweaks a parameter?

I don't need live web updates or anything like that.
The results would stay on the machine running the simulation. All I would need is for my users to be able to change the parameters.

This doesn't sound like "tweaking" to me. To me, tweaking implies that the user gets to see the results of their changes. Or do you just mean that the user doesn't need changes from the ongoing execution of the simulation pushed to their browser automatically?

At a minimum, you'll need an HTTP server. That HTTP server will need to serve a static web page with a form with fields for the inputs users are allowed to make. That form will have to submit those inputs back to the web server. There will need to be a CGI script or some other request handler that parses the request parameters passed along by the web server and executes your simulation with the provided parameters and then provides some sort of HTML response back to the web server to return to the client. I think the python standard library has a CGI module to help with this sort of thing.

The CGI approach will be fine for situations when the simulation executes for a relatively short period of time after receiving new inputs. It will work less well if the simulation is running continuously.

In that case, you will need a long-running process for your simulation, and then a way to communicate between your simulation and an HTTP server.

One way of doing that is to build a request handler directly into you your simulation, or, build your simulation directly into a request handler. You'd probably want to start with a minimal python web framework. Django sounds like overkill...maybe something like Flask, or Bottle. Follow their recommended practices for deployment.

How does your simulation run now? How do people interact with it if it is running locally?
posted by Good Brain at 1:55 PM on May 22, 2013 [2 favorites]


Assuming that you don't have that many users and that disk-space is no real issue, you could use small text-files to safe the users' inputs and to read from them to change the parameters in your script. All you would need is a loop that checks a directory for an updated file since the last run. It might be good to use timestamps in the filenames for 'historical' reasons. Of course you would need a simple way to safe the data from a webform to those files, but that should be trivial.

BUT: this is very basic, definitely not secure, and surely not even a good idea, more like a bad hack. But it could work and gets the job done.
posted by KMB at 2:04 PM on May 22, 2013


Here's the simplest way I can think of: Write a PHP page that accepts the parameters, and presumably a fixed password. This doesn't have to be any more complicated than a simple Web form that updates values in a database. For a database you can just use something simple like SQLite (if the webserver and simulator are the same machine), or MySQL if that's what your webhosting provider has. Then, in your simulation, check the database at intervals to read the current values. Bam. If your simulation is running on a different machine than the webserver, you can simply write another PHP page that exports the settings in JSON or XML, and have the simulation query that page for updates.

You can do this using Flask or similar if you'd prefer to use Python, but PHP is much easier to deploy.
posted by neckro23 at 2:27 PM on May 22, 2013


I think the fastest thing to code would be to have the python simulator program periodically check a set of files on the disk that contain the parameters. Your collaborators could then just scp updated files to the simulator machine. Then when the program next checks the parameter files, it will load the new parameters.
posted by Salvor Hardin at 2:44 PM on May 22, 2013


Never underestimate the power of a bored PHP developer. I went ahead and made a working example, using MySQL. If you append "?json" to the script's URL you'll get JSON back. Password is required to update the parameters. The MySQL table is a simple two-column key/value store.

It's very bare-bones and doesn't do any error checking, but it should be secure enough. Only parameter keys in the $params array are allowed in the database.
posted by neckro23 at 3:40 PM on May 22, 2013


SSH?
posted by oceanjesse at 3:49 PM on May 22, 2013 [1 favorite]


Download Crunchy, create user accounts for it using account_manager.py and have Crunchy run on your server. You can create a simple web page with an embedded editor that will launch individual copies of your simulator with parameters that can be adjusted by the users.
posted by aroberge at 5:57 PM on May 22, 2013


Response by poster: Wow! Thanks to all!

And, neckro23 thanks for writing that! It's over my head but, I think I'll take a look and try to get it up!
posted by millerizer at 6:37 AM on May 23, 2013


« Older What to do with this body?   |   How Can I Be Notified of Aurora Sightings in NY... Newer »
This thread is closed to new comments.