Skip to content

Instantly share code, notes, and snippets.

@jonforums
Created March 12, 2011 19:52
Show Gist options
  • Save jonforums/867504 to your computer and use it in GitHub Desktop.
Save jonforums/867504 to your computer and use it in GitHub Desktop.
CherryPy StatsPage usage
import cherrypy
from cherrypy.lib import cpstats
from jinja2 import Environment, DictLoader
INDEX_TMPL = '''
<!DOCTYPE html>
<html>
<body>
<h1 style="color:#990000;width:440px;margin:0 auto;">Hello {{name}}!</h1>
</body>
</html>
'''
jenv = Environment(loader=DictLoader({ 'index.html': INDEX_TMPL }))
class Root(object):
@cherrypy.expose
@cherrypy.config(**{'response.headers.Content-Type':'text/html; charset:utf-8'})
def index(self, name=None):
tmpl = jenv.get_template('index.html')
name = name.title() if name else 'Anonymouse'
ctx = { 'name' : name }
return tmpl.render(ctx)
@cherrypy.expose
@cherrypy.config(**{'tools.json_out.on':True})
def index_json(self, name=None):
return self.index(name)
@cherrypy.expose
def name(self, name=None):
return self.index(name)
# lean-n-mean
cherrypy.engine.timeout_monitor.unsubscribe()
cherrypy.engine.autoreload.unsubscribe()
if hasattr(cherrypy.engine, 'console_control_handler'):
cherrypy.engine.console_control_handler.subscribe()
cherrypy.config.update({
'server.socket_host' : '127.0.0.1',
'server.socket_port' : 8080,
'log.screen' : True
})
root = Root()
root.stats = cpstats.StatsPage()
cherrypy.tree.mount(root, '/', { '/' : { 'tools.cpstats.on':True } })
cherrypy.engine.start()
cherrypy.engine.block()
[12/Mar/2011:12:59:10] ENGINE Bus STARTED
127.0.0.1 - - [12/Mar/2011:12:59:15] "GET /stats HTTP/1.1" 301 111 ...
[12/Mar/2011:12:59:15] HTTP Traceback (most recent call last):
File "C:\Python27\lib\site-packages\cherrypy\_cprequest.py", line 645, in respond
response.body = self.handler()
File "C:\Python27\lib\site-packages\cherrypy\lib\encoding.py", line 228, in __call__
ct.params['charset'] = self.find_acceptable_charset()
File "C:\Python27\lib\site-packages\cherrypy\lib\encoding.py", line 142, in find_acceptble_charset
if encoder(self.default_encoding):
File "C:\Python27\lib\site-packages\cherrypy\lib\encoding.py", line 86, in encode_string
for chunk in self.body:
File "C:\Python27\lib\site-packages\cherrypy\lib\cpstats.py", line 493, in index
for title, scalars, collections in self.get_namespaces():
File "C:\Python27\lib\site-packages\cherrypy\lib\cpstats.py", line 552, in get_namespaces
s = extrapolate_statistics(logging.statistics)
File "C:\Python27\lib\site-packages\cherrypy\lib\cpstats.py", line 195, in extrapolate_statistics
v = extrapolate_statistics(v)
File "C:\Python27\lib\site-packages\cherrypy\lib\cpstats.py", line 199, in extrapolate_statistics
v = v(scope)
File "C:\Python27\lib\site-packages\cherrypy\wsgiserver\__init__.py", line 1627, in <lambda>
in s['Worker Threads'].values()], 0),
File "C:\Python27\lib\site-packages\cherrypy\wsgiserver\__init__.py", line 1354, in <lambda>
'Bytes Read': lambda s: self.bytes_read + ((self.start_time is None) and 0 or self.conn.rfile.bytes_read),
AttributeError: 'NoneType' object has no attribute 'rfile'
[14/Mar/2011:09:04:16] HTTP Traceback (most recent call last):
File "C:\Python26\lib\site-packages\cherrypy\_cprequest.py", line 645, in respond
response.body = self.handler()
File "C:\Python26\lib\site-packages\cherrypy\lib\encoding.py", line 228, in __call__
ct.params['charset'] = self.find_acceptable_charset()
File "C:\Python26\lib\site-packages\cherrypy\lib\encoding.py", line 161, in find_acceptable_charset
if encoder(encoding):
File "C:\Python26\lib\site-packages\cherrypy\lib\encoding.py", line 86, in encode_string
for chunk in self.body:
File "C:\Python26\lib\site-packages\cherrypy\lib\cpstats.py", line 493, in index
for title, scalars, collections in self.get_namespaces():
File "C:\Python26\lib\site-packages\cherrypy\lib\cpstats.py", line 552, in get_namespaces
s = extrapolate_statistics(logging.statistics)
File "C:\Python26\lib\site-packages\cherrypy\lib\cpstats.py", line 195, in extrapolate_statistics
v = extrapolate_statistics(v)
File "C:\Python26\lib\site-packages\cherrypy\lib\cpstats.py", line 199, in extrapolate_statistics
v = v(scope)
File "C:\Python26\lib\site-packages\cherrypy\wsgiserver\__init__.py", line 1627, in <lambda>
in s['Worker Threads'].values()], 0),
File "C:\Python26\lib\site-packages\cherrypy\wsgiserver\__init__.py", line 1354, in <lambda>
'Bytes Read': lambda s: self.bytes_read + ((self.start_time is None) and 0 or self.conn.rfile.bytes_read),
AttributeError: 'NoneType' object has no attribute 'rfile'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment