Static URL in cherrypy
Asked Answered
T

1

9

I have a website server based on python (cherrypy), and i need some help. I'm sorry in advance if this question is too basic. I don't have a vast of experience in this area so far.

My main page is on http://host:9090/home/static/index.html. I want to rewrite the address of above, and define the following address as the main page: http://host:9090/home/. The code itself suppose to stay in the same place. I just want a shorter link so /home/static/index.html will be available also in /home/.

Is rewrite URL is what i need? If so, I've found the following link, but unfortunately i don't know how to implement it in my code: http://www.aminus.org/blogs/index.php/2005/10/27/url_rewriting_in_cherrypy_2_1?blog=2

 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'
             },
        '/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

    webapp = Root(args.target)
    cherrypy.quickstart(webapp, '/home', conf)

Anyone can help?

Thomasenathomasin answered 7/12, 2015 at 9:24 Comment(1)
@qarma As i mentioned above, i need rewrite URL.Thomasenathomasin
E
3

In my projects, I usually point '/' directly to the static folder. I prefer to omit all appearances of 'static' in my URLs, and imho it's a good practice to serve a resource only through exactly one URL. Anyway, it could be a simple solution to manually write the mapping, if the same static resource has to be served through different URLs.

For example, the folder structure looks as follows:

repo \
    __init__.py
    main.py
    static \
        test \
            some-module.js

It's handy to have the path to the root directory as a global variable, here I call it SITE_ROOT.

SITE_ROOT = '/home/user/repo'
conf = {
    '/': {
        'tools.staticdir.root': os.path.join(SITE_ROOT, 'static')
    },
    '/test': {
        'tools.staticdir.on': True,
        'tools.staticdir.dir': 'test'
    },
    '/static/test': {
        'tools.staticdir.on': True,
        'tools.staticdir.dir': 'test'
    },
}

Now both URLs lead to the same static resource, without redirection.

http://127.0.0.1:8080/test/some-module.js
http://127.0.0.1:8080/static/test/some-module.js

Further reading:

https://cherrypy.readthedocs.org/en/3.3.0/progguide/files/static.html#forming-urls

Encephalograph answered 14/12, 2015 at 1:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.