如何在具有某个变量的值时自动重新加载网页?

时间:2013-01-20 14:41:54

标签: python html cherrypy page-refresh

在下面的python / html脚本中,我想在变量“condition”的值等于“1”时自动刷新网页。这样页面显示新文本自动包含变量“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)

1 个答案:

答案 0 :(得分:1)

好的,你可以从服务器上获取你需要一些ajax的文本。

试一试......

#!/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)

希望这有帮助!

安德鲁