Static URL in cherrypy

816 views
Skip to first unread message

SuperTwister

unread,
Dec 8, 2015, 8:06:40 AM12/8/15
to cherrypy-users

My web site works with a cherrypy server, and HTML/JS client. I want that that my website will have a static link, so no matter which page will be visited, the URL on the browser will be stay the same.

Currently the link is:

/static/index.html

I want that the link will be:

/home

Here is the website configuration:

class Root(object):
    def __init__(self, target):
        self.target_server = target

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

I have tried to change cherrypy.quickstart(webapp, '/', conf) to cherrypy.quickstart(webapp, '/home', conf), but it's just changed the link to /home/static/index.html.

Any idea?


Stefan Krüger

unread,
Dec 10, 2015, 8:01:47 AM12/10/15
to cherrypy-users
Hi SuperTwister,

have a look at

Tutorial 2: Different URLs lead to different functions

 if you modifie the example as this:
import random
import string

import cherrypy

class MyHome(object):
   
@cherrypy.expose
   
def index(self):
       
raise cherrypy.HTTPRedirect("/home")

   
@cherrypy.expose
   
def home(self):
       
return "hello world"

if __name__ == '__main__':
   
cherrypy.quickstart(MyHome())

you should get to the home path every time you try to access your route.

to server static things have a look at
Static content serving - Serving a whole directory

iam using something like this for my static things:
.......

dir_current = os.path.dirname(os.path.abspath(__file__))

......

class StaticFiles(object):

    """only there to have a class..."""

    pass
    # _cp_config = {
    #     'tools.staticdir.on': True,
    #     # 'tools.staticdir.root': dir_static,
    #     # 'tools.staticdir.dir': '',
    #     'tools.staticdir.index' : 'index.html'
    # }
#


....

# config static tool
    dir_static = os.path.join(dir_current, 'static')
    configStatic = {
        '/': {
            'tools.staticdir.on': True,
            'tools.staticdir.root': dir_static,
            'tools.staticdir.dir': '',
            'tools.staticdir.index': 'index.html'
        },
    }

    cherrypy.tree.mount(StaticFiles(), '', configStatic)


....

    # start cherrypy
    print('')
    print('******************************************')
    print('')
    print('cherrypy server - start engine:')
    cherrypy.engine.start()
    cherrypy.engine.block()


eventually that helps you?!

sunny greetings
stefan

SuperTwister

unread,
Dec 13, 2015, 5:52:59 AM12/13/15
to cherrypy-users
Hi Stefan Krüger, It's doesn't works. 
I've tried to use 
[/static]
tools.staticdir.on = True
tools.staticdir.dir = "/
home/site/static"
tools.staticdir.index = "
index.html"
But it didn't work.

Please have a look at my code. Do you know what could be the issue?

cherrypy.config.update({
                       
'server.socket_port': 9090,
                       
'server.socket_host': '0.0.0.0'
                       
})
conf
= {
   
'/': {
           
'tools.sessions.on': True,
           
'tools.staticdir.root': os.path.abspath(os.getcwd())
         
},
   
'/static': {
           
'tools.staticdir.on': True,
           
'tools.staticdir.dir': './static/html'
           
'tools.staticdir.index': 'index.html'
         
},
   
'/js': {
           
'tools.staticdir.on': True,
           
'tools.staticdir.dir': './static/js'
         
},
   
'/css': {
           
'tools.staticdir.on': True,
           
'tools.staticdir.dir': './static/css'
         
},
   
'/img': {
           
'tools.staticdir.on': True,
           
'tools.staticdir.dir': './static/img'
         
},
   
'/fonts': {
           
'tools.staticdir.on': True,
           
'tools.staticdir.dir': './static/fonts'

   
}


}


class Root(object):
   
def __init__(self, target):
       
self.target_server =
target


   
@cherrypy.expose
   
def index(self):
       
raise cherrypy.HTTPRedirect("/static/index.html")


   cherrypy
.quickstart(webapp, '/home', conf)

Stefan Krüger

unread,
Dec 17, 2015, 3:15:34 AM12/17/15
to cherrypy-users
Hi SuperTwister,

i think your code can't work.
you have a 'webapp' in your quickstart but no place you define it..
pleas do some work on your own:
build a minimal one-file example that is runnable.

you can start with on of the documentation examples:
http://docs.cherrypy.org/en/latest/basics.html?highlight=quickstart#the-one-minute-application-example
(http://docs.cherrypy.org/en/latest/pkg/cherrypy.html?highlight=quickstart#cherrypy.quickstart)

and than extend it first so that the basic static serving works. (like in the documentation)
and if you have this running copy&save it with a new file name and try to include what you want to achieve.

and than one more thing:
But it didn't work.
says nothing.
Does the script run or is there a startup error?
are there runtime errors?
what is happening when you try to do what you want?

don't get me wrong - i am willing to help - but i have not the time to do it all for you..

sunny greetings
stefan

Kearney Taaffe

unread,
Mar 17, 2016, 3:32:57 PM3/17/16
to cherrypy-users
Hopefully this helps you.

Here's a simple site I created just to serve static web-content. 

DIRECTORY STRUCTURE
public/
   css/
       style.css
   js/
   views/
       index.html
server.py

and here's the code I have in my server.py file

import os
import cherrypy

class Root(object):
    pass       

if __name__ == '__main__':
    root = os.path.dirname(os.path.abspath(__file__))  # this file's directory
    print root
        
    cherrypy.config.update( {
        'server.socket_host': '0.0.0.0',
        'server.socket_port': 8080,
    } )
    
    conf = {
        '/': {
            'tools.staticdir.on': True,
            'tools.staticdir.dir': root + '/public',
            'tools.staticdir.index': '/public/views/index.html'
        }
    }
    cherrypy.quickstart(Root(), '/', conf)
Reply all
Reply to author
Forward
0 new messages