Omr*_*mri 9 python url cherrypy
我有一个基于python(cherrypy)的网站服务器,我需要一些帮助.如果这个问题太基础,我很抱歉.到目前为止,我在这方面没有丰富的经验.
我的主页面已打开http://host:9090/home/static/index.html.我想重写上面的地址,并将以下地址定义为主页:http://host:9090/home/.代码本身假设保持在同一个地方.我只想要一个更短的链接,所以/home/static/index.html也可以使用/home/.
重写URL是我需要的吗?如果是这样,我发现了以下链接,但不幸的是我不知道如何在我的代码中实现它:http: //www.aminus.org/blogs/index.php/2005/10/27/url_rewriting_in_cherrypy_2_1?博客= 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)
Run Code Online (Sandbox Code Playgroud)
有人可以帮忙吗?
在我的项目中,我通常'/'直接指向静态文件夹。我更喜欢忽略'static'URL 中出现的所有内容,恕我直言,仅通过一个 URL 提供资源是一种很好的做法。无论如何,如果必须通过不同的 URL 提供相同的静态资源,手动编写映射可能是一个简单的解决方案。
例如,文件夹结构如下所示:
repo \
__init__.py
main.py
static \
test \
some-module.js
Run Code Online (Sandbox Code Playgroud)
将根目录的路径作为全局变量很方便,这里我称之为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'
},
}
Run Code Online (Sandbox Code Playgroud)
现在,两个 URL 都指向相同的静态资源,无需重定向。
http://127.0.0.1:8080/test/some-module.js
http://127.0.0.1:8080/static/test/some-module.js
Run Code Online (Sandbox Code Playgroud)
进一步阅读:
https://cherrypy.readthedocs.org/en/3.3.0/progguide/files/static.html#forming-urls
| 归档时间: |
|
| 查看次数: |
561 次 |
| 最近记录: |