Search code examples
pythoncherrypy

cherrypy catch all route


Using Cherrypy how do I make this script so that any url will load the load index.html

example if any of these are used then they all load the index.html page www.mtsite.test/111111/22222/333333 www.mtsite.test/1 www.mtsite.test/fred www.mtsite.test/test

import os, os.path
import random
import string

import cherrypy

class StringGenerator(object):
   @cherrypy.expose
   " def *(self)
   def index(self):
       return file('index.html')

if __name__ == '__main__':
    conf = {
        '/': {
            'tools.sessions.on': True,
            'tools.staticdir.root': os.path.abspath(os.getcwd())
        },
        '/static': {
            'tools.staticdir.on': True,
            'tools.staticdir.dir': './public'
        }
    }


    webapp = StringGenerator()
    cherrypy.quickstart(webapp, '/', conf)

Solution

  • #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    
    
    import cherrypy
    
    
    config = {
      'global' : {
        'server.socket_host' : '127.0.0.1',
        'server.socket_port' : 8080,
        'server.thread_pool' : 4
      }
    }
    
    
    class App:
    
      @cherrypy.expose
      def default(self, *args, **kwargs):
        return u'It is me again at {0} with {1}'.format(args, kwargs)
    
    
    if __name__ == '__main__':
      cherrypy.quickstart(App(), '/', config)