Static html files in cherrypy

I am having a problem with what should be the basic concept in cherrypy, but so far I have not been able to find a tutorial or an example on how to do this (I am new to Cherrypy, be careful).

Problem. (This is a test part, therefore, the lack of reliable authentication and sessions in the code)

The user goes to the index.html page, which is the login page in which the data is indicated and if the data does not match what is in the file, the error message is returned and displayed. It works! If the data is correct, another html file (network.html) is displayed for the user. This is a bit that I can’t get.

The current file system looks like this: -

AppFolder - main.py (main CherryPy file) - media (folder) - css (folder) - js (folder) - index.html - network.html 

The layout of the files seems to be correct, since I can access index.html. The code is as follows: (I have a comment where I try to return a new page)

 import cherrypy import webbrowser import os import simplejson import sys from backendSystem.database.authentication import SiteAuth MEDIA_DIR = os.path.join(os.path.abspath("."), u"media") class LoginPage(object): @cherrypy.expose def index(self): return open(os.path.join(MEDIA_DIR, u'index.html')) @cherrypy.expose def request(self, username, password): print "Debug" auth = SiteAuth() print password if not auth.isAuthorizedUser(username,password): cherrypy.response.headers['Content-Type'] = 'application/json' return simplejson.dumps(dict(response ="Invalid username and/or password")) else: print "Debug 3" #return network.html here class DevicePage(object): @cherrypy.expose def index(self): return open(os.path.join(MEDIA_DIR, u'network.html')) config = {'/media': {'tools.staticdir.on': True, 'tools.staticdir.dir': MEDIA_DIR, }} root = LoginPage() root.network = DevicePage() # DEVELOPMENT ONLY: Forces the browser to startup, easier for development def open_page(): webbrowser.open("http://127.0.0.1:8080/") cherrypy.engine.subscribe('start', open_page) cherrypy.tree.mount(root, '/', config = config) cherrypy.engine.start() 

Any help or guidance in this matter would be greatly appreciated.

Greetings

Chris

+8
python cherrypy web-applications
source share
1 answer

You have basically two options. If you want the user to visit /request and return this network.html content, just return it:

 class LoginPage(object): ... @cherrypy.expose def request(self, username, password): auth = SiteAuth() if not auth.isAuthorizedUser(username,password): cherrypy.response.headers['Content-Type'] = 'application/json' return simplejson.dumps(dict(response ="Invalid username and/or password")) else: return open(os.path.join(MEDIA_DIR, u'network.html')) 

Another approach is for the user to visit /request and, if allowed, redirected to the content at a different URL, possibly /device :

 class LoginPage(object): ... @cherrypy.expose def request(self, username, password): auth = SiteAuth() if not auth.isAuthorizedUser(username,password): cherrypy.response.headers['Content-Type'] = 'application/json' return simplejson.dumps(dict(response ="Invalid username and/or password")) else: raise cherrypy.HTTPRedirect('/device') 

Then their browser will make a second request for the new resource.

+5
source share

All Articles