How to create a web interface for a daemon written in Python?

Graham Dumpleton grahamd at dscpl.com.au
Tue May 14 20:00:51 EDT 2002


Markus <listuser at nicetomeetyou.to> wrote in message news:<aboknd$o52$00$1 at news.t-online.com>...
> Hi all,
> 
> we're using a Python script as a watchdog for another process.
> The watchdog is running in console mode, so it is necessary to access the 
> terminal it is running on to operate the program.
> 
> I'm thinking about a remote control feature via a web interface, running the 
> script in the background. The interface simply has to provide access to the 
> 3 basic functions of the watchdog script, i.e. "start", "stop" and "reset", 
> all of which don't need any parameters.
> 
> Can anybody give me a hint about the best way to do this? (Or even better, a 
> hint to a working implementation of something similar ...)

On the basis that all the watchdog component does is run periodically and
do some check each time, you might try the following working example using
OSE. Note that OSE isn't pure Python however and you might find that not to
your liking.

Anyway, the example in this instance uses XML-RPC over HTTP to stop and start
the timer which is going off every five seconds. OSE also has a HTTP servlet
framework so you could instead write a CGI like component so that normal
browsers might control things, rather than requiring a special XML-RPC client.

Example client code using the OSE XML-RPC client would be:

import netrpc.xmlrpc

service = netrpc.xmlrpc.RemoteService("http://localhost:8080/MONITOR")
service.stop()
service.start()

The actual code for the server side follows. OSE itself is available at
"http://ose.sourceforge.net". There is an extensive manual (100+ pages)
on the Python wrappers.

import netsvc
import netsvc.xmlrpc
import signal

class Monitor(netsvc.Service):

  DELAY = 5

  def __init__(self,name="MONITOR"):
    netsvc.Service.__init__(self,name)
    self.joinGroup("web-services")
    self.exportMethod(self.stop)
    self.exportMethod(self.start)
    self.startTimer(self.wakeup,Monitor.DELAY,"watchdog")

  def wakeup(self,tag):
    print "wakeup"
    # do the actual work required here
    self.startTimer(self.wakeup,Monitor.DELAY,"watchdog")

  def stop(self):
    print "stop"
    self.cancelTimer("watchdog")

  def start(self):
    print "start"
    # old timer will automatically be cancelled
    self.startTimer(self.wakeup,Monitor.DELAY,"watchdog")


dispatcher = netsvc.Dispatcher()

dispatcher.monitor(signal.SIGINT)
dispatcher.monitor(signal.SIGHUP)

service = Monitor()

httpd = netsvc.HttpDaemon(8080)
rpcgw = netsvc.xmlrpc.RpcGateway("web-services")
httpd.attach("/",rpcgw)
httpd.start()

dispatcher.run()



More information about the Python-list mailing list