Enter the following code and run it. If you point your web browser to the familiar http://localhost:8080
address, you will see something similar to the picture below with the time changing every five seconds or so. (The code is also available as timer.py
)
Our small CherryPy application offers just two methods (both highlighted in the code). The index()
method returns a minimalistic HTML page with some static text and a small piece of JavaScript that takes care of retrieving the current time from the server. It also features a time()
method that simply returns the current time as plain text.
Chapter4/timer.py
import cherrypy
import os.path
from time import asctime
current_dir = os.path.dirname(os.path.abspath(__file__))
class Root(object):
@cherrypy.expose
def index(self):
return '''<html>
<head><script type="text/javascript" src="/jquery.js" ></script></head>
<body>...