You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何配置CherryPy从index目录及静态目录提供静态文件并支持自定义路径

Got it, let's get your CherryPy setup working exactly how you want it. Here's a tailored solution for your project structure:

Project Structure Recap

First, just to align on what we're working with:

project/
  index/
    about.html
    index.html
    forum.html
    profile.html
    settings.html
    apple-touch-icon.png
  static/
    main.css
    forum.css
    main.js
    forum.js
    load-image.min.js
  server.py
  metaclass.py
  mailing.py
  errors.log

CherryPy Configuration & Code

Update your server.py with this code to map clean URLs and serve all your static content properly:

import cherrypy

class Root:
    # No custom methods needed here—we're using CherryPy's built-in static file handlers
    pass

if __name__ == '__main__':
    # Core configuration for URL mapping and static file serving
    app_config = {
        # Map root URL (/) directly to index/index.html
        '/': {
            'tools.staticdir.root': './',
            'tools.staticfile.on': True,
            'tools.staticfile.filename': 'index/index.html'
        },
        # Map clean, short URLs to their corresponding HTML files
        '/about': {
            'tools.staticfile.on': True,
            'tools.staticfile.filename': 'index/about.html'
        },
        '/forum': {
            'tools.staticfile.on': True,
            'tools.staticfile.filename': 'index/forum.html'
        },
        '/profile': {
            'tools.staticfile.on': True,
            'tools.staticfile.filename': 'index/profile.html'
        },
        '/settings': {
            'tools.staticfile.on': True,
            'tools.staticfile.filename': 'index/settings.html'
        },
        # Serve all other files in index/ (like apple-touch-icon.png) via /index/...
        '/index': {
            'tools.staticdir.on': True,
            'tools.staticdir.dir': 'index'
        },
        # Serve your CSS/JS assets from the static/ directory
        '/static': {
            'tools.staticdir.on': True,
            'tools.staticdir.dir': 'static'
        }
    }

    # Start the CherryPy server with our configuration
    cherrypy.quickstart(Root(), '/', app_config)

How This Works

Let's break down the key parts:

  • Clean URL Mapping: Each entry like /about uses CherryPy's staticfile tool to directly link the short URL to its corresponding HTML file in the index/ folder. Users won't need to type /index/about.html anymore.
  • Root URL Handling: The / configuration ensures visiting your site's root loads index/index.html automatically.
  • Remaining Index Files: The /index section makes sure other assets in the index/ directory (like apple-touch-icon.png) are still accessible via their original paths (e.g., yoursite.com/index/apple-touch-icon.png).
  • Static Assets: The /static section serves your CSS and JS files exactly as you'd expect (e.g., yoursite.com/static/main.css).

Optional: Make All Index Files Accessible From Root

If you want to hide the /index path entirely (so apple-touch-icon.png is accessible at yoursite.com/apple-touch-icon.png instead), adjust the root configuration like this:

'/': {
    'tools.staticdir.root': './',
    'tools.staticdir.on': True,
    'tools.staticdir.dir': 'index',
    # Ensure root URL loads index.html by default
    'tools.staticfile.index': 'index.html'
},
# Keep the clean URL mappings for /about, /forum, etc.
'/about': {
    'tools.staticfile.on': True,
    'tools.staticfile.filename': 'index/about.html'
},
# ... rest of the config stays the same

This way, every file in index/ is available directly from the root, while your clean URLs still take priority for the main pages.

Just run python server.py from your project/ directory, and you're good to go!

内容的提问来源于stack exchange,提问作者Robert Moore

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 04:24:28