Skip to content

Instantly share code, notes, and snippets.

@earthday
Created May 5, 2019 17:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save earthday/0b2d049e85bf7ec91ab168094224f9f9 to your computer and use it in GitHub Desktop.
Save earthday/0b2d049e85bf7ec91ab168094224f9f9 to your computer and use it in GitHub Desktop.
# https://stackoverflow.com/questions/13484206/cherrypy-url-dispatcher
import cherrypy
class City:
def __init__(self, name):
self.name = name
self.population = 10000
# @cherrypy.expose
def index(self, **kwargs):
return "Welcome to %s, pop. %s" % (self.name, self.population)
# @cherrypy.expose
def update(self, **kwargs):
self.population = kwargs['pop']
return "OK"
# @cherrypy.expose
def show_date(self, year, month, day):
return "{0}/{1}/{2}".format(year, month, day)
# @cherrypy.expose
def category_list(self):
return "category_list"
# @cherrypy.expose
def home(self, section):
return "home" + section
d = cherrypy.dispatch.RoutesDispatcher()
d.connect(action='index', name='hounslow', route='/hounslow', controller=City('Hounslow'))
d.connect(action='index', name='surbiton', route='/surbiton', controller=City('Surbiton'),
conditions=dict(method=['GET']))
d.mapper.connect('/surbiton', controller='surbiton',
action='update', conditions=dict(method=['POST']))
d.mapper.connect('/date/year/:year/month/:month/day/:day', controller='surbiton',
action='show_date', requirements={'month': '\d{1,2}'})
conf = {'/': {'request.dispatch': d}}
cherrypy.config.update({'server.socket_port': 5000})
cherrypy.tree.mount(root=None, config=conf)
cherrypy.engine.start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment