In the following python/html script,I want to refresh the web page automatically when the value of the variable "condition" equal to "1". So that the page show me the new text contains of the variable "myText" automatically. The variable "myText" can have any text, and the following script is just an example.
#!/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)
Ok for you to get your text from the server you're going to need some ajax.
Give this a try...
#!/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):
html = """
<html>
<head>
</head>
<body>
<script language="javascript" type="text/javascript">
function getMyText()
{
// code for IE7+, Firefox, Chrome, Opera, Safari
if(window.XMLHttpRequest)
xmlhttp=new XMLHttpRequest();
else// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById('message').innerHTML= = xmlhttp.responseText;
}
}
xmlhttp.open("GET","/getMyText?logout=True", true);
xmlhttp.send();
}
</script>
<p id="message"><p>
<a href="?logout=1" onclick="return var r = confirm('Are you sure you want to logout?');if (r==true){getMyText(condition);"><img src="images/Logout.png"></a>
</ul>
</body>
</html>
"""
def getMyText(logout=False)
myText = "Hello"
if logout:
self.led_logout = int(logout)
if self.led_logout:
print "Logout !!!!!"
AuthController().logout('/?logout=0')
return 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)
Hope this helps!
Andrew