次のpython/htmlスクリプトでは、変数「condition」の値が「1」に等しいときにWebページを自動的に更新したいと思います。ページに変数「myText」の新しいテキストが自動的に含まれるように表示されます。変数「myText」には任意のテキストを含めることができます。次のスクリプトは単なる例です。
#!/usr/bin/python
import cherrypy
import os.path
import struct
from auth import AuthController, require, member_of, name_is
import subprocess
import commands
class Server(object):
led_logout=0
led_restart=0
condition=0
_cp_config = {
'tools.sessions.on': True,
'tools.auth.on': True
}
auth = AuthController()
@cherrypy.expose
@require()
def index(self,logout=''):
html = """
<html>
<head>
</head>
<body>
<p>{htmlText}
<p>
<a href="?logout=1" onclick="return confirm('Are you sure you want to logout?');"><img src="images/Logout.png"></a>
</ul>
</body>
</html>
"""
myText = ''
myText = "Hello"
if logout:
self.led_logout = int(logout)
if self.led_logout:
print "Logout !!!!!"
AuthController().logout('/?logout=0')
return html.format(htmlText=myText)
index.exposed = True
#configuration
conf = {
'global' : {
'server.socket_host': '0.0.0.0', #0.0.0.0 or specific IP
'server.socket_port': 8085 #server port
},
'/images': { #images served as static files
'tools.staticdir.on': True,
'tools.staticdir.dir': os.path.abspath('/home/ubuntu/webserver/images')
}
}
cherrypy.quickstart(Server(), config=conf)