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?
Deployment of CherryPy application is considered to be quite an easy method where all the required packages are available from the Python system path. In shared web-hosted environment, web server will reside in the front end which allows the host provider to perform the filtering actions. The front-end server can be Apache or lighttpd.
CherryPy allows any HTTP method. CherryPy handles the combinations of HTTP versions between the client and the setting set for the server. CherryPy is designed based on the multithreading concept. Every time a developer gets or sets a value into the CherryPy namespace, it is done in the multi-threaded environment.
CherryPy is a web framework of Python which provides a friendly interface to the HTTP protocol for Python developers. It is also called a web application library. CherryPy uses Python’s strengths as a dynamic language to model and bind HTTP protocol into an API.
cherrypy.server − It configures and controls the WSGI or HTTP server. cherrypy.tools − A toolbox of utilities that are orthogonal to processing an HTTP request. RESTful web service implements each section of CherryPy architecture with the help of the following − Authentication helps in validating the users with whom we are interacting.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With