0

I am trying have a set of python scripts report their status to a set of micro controllers.

So my idea for this is to have the python scripts each create their own webpage that can be viewed by the micro controllers, but is there anyway to have the script itself keeping the page served, i.e. an apache library so that if the script crashes or is not running the page is not served or a way to make the page have a default value if the script is not running.

Jonny Flowers
  • 631
  • 1
  • 9
  • 20

2 Answers2

1

You could use http://docs.python.org/library/simplehttpserver.html or some minimal http server framework like http://flask.pocoo.org/ or http://www.cherrypy.org/.

If you want to feed "live" information to your micro controllers also have a look at comet style long polling requests. You essentially keep downloading "the page" forever and analyse it as a data stream while the server keeps adding updated info at the "end of the page".

snies
  • 3,461
  • 1
  • 22
  • 19
  • simplehttpserver seems to be the way forward, however is there a way to implement it into my code to make it nonblocking as I want it to be the first thing it does and at the moment it is waiting to print out every time someone accesses a page, I don't care about that, can I disable that? – Jonny Flowers May 16 '12 at 21:19
  • this stackoverflow question might help: http://stackoverflow.com/questions/2455606/basichttpserver-simplehttpserver-and-concurrency – snies May 16 '12 at 23:36
1

You can also have a look at twisted.web

A very basic example:

from twisted.web.server     import Site
from twisted.web.resource   import Resource
from twisted.internet       import reactor

class StatusPageResource(Resource):
    isLeaf           = True

    def __init__(self, param1):
        self.param1 = param1
        # Call the constructor of the super class
        Resource.__init__(self)

    def render_GET(self, request):
        return "<html><body>%s</body></html>" % self.param1


my_res = Resource()
my_res.putChild('GetStatusPage1', StatusPageResource(param1='abc'))
my_res.putChild('GetStatusPage2', StatusPageResource(param1='xyz'))

factory = Site(my_res)
reactor.listenTCP(8080, factory)
print 'Runnning on port 8080'
reactor.run()

Now point your browser to http://localhost:8080/GetStatusPage1 (for example)

E.Z.
  • 6,393
  • 11
  • 42
  • 69
  • I tried this and could not get it to work properly it came up with massive amounts of errors. Thanks anyway – Jonny Flowers May 16 '12 at 21:17
  • @Jonny Flowers: it works just fine on my environment. Dumb question: do you have twisted at all? It's not part of the standard Python library... – E.Z. May 18 '12 at 10:44
  • I didn't have it so downloaded it and with the code you gave me threw up a load of things about using the wrong instances – Jonny Flowers May 19 '12 at 15:05
  • Loads of messages about using the @implementor class decorator instead. Any idea? But the pages get hosted. – Jonny Flowers May 19 '12 at 15:13
  • Also how would I get it to continue with other actions afterwards. I want the web server to say whether the rest of the script is running. The current function is that it listens for data and then puts it into a database. – Jonny Flowers May 19 '12 at 16:04
  • Hi @Jonny. I'm not sure I understood your requirements completely, but maybe you need to run _"the rest of the script"_ in a separate thread, while the _web server_ part using `twisted` is only used to report some sort of status? If that's the case you may want to have a look at [the](http://www.google.com/search?q=python+threading+tutorial) [threading](http://stackoverflow.com/questions/tagged/python+multithreading) [module](http://docs.python.org/library/threading.html). – E.Z. May 21 '12 at 09:42
  • Oh ok, so from what I understand from that I would be creating another process that runs concurrently so that if one crashes both would stop? – Jonny Flowers May 22 '12 at 13:28
  • Hm, not exactly: everything would run under a single `python` _process_, but on different [_threads_](http://en.wikipedia.org/wiki/Thread_%28computing%29). If a thread crashes, the others do not necessarily have to be affected; for example if the thread running _"the rest of your script"_ crashes, the one running your `twisted.web` server would probably still be up answering your HTTP requests. The exact behaviour will depend on how you design your solution. – E.Z. May 22 '12 at 14:10
  • Oh ok. What I would want to happen is that if my script goes down the web server comes with it. – Jonny Flowers May 22 '12 at 17:32