0

在以下 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)
4

1 回答 1

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)

希望这可以帮助!

安德鲁

于 2013-01-20T18:16:04.400 回答