Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static html Files in Cherrypy

I am having an issue with what should be a basic concept in cherrypy but as yet I have been unable to find a tutorial or example on how to do this (I am a Cherrypy newbie, be gentle).

The Problem. (This is a Test piece hence the lack of robust authentication and sessions in the code)

The user goes to the index.html page which is a login page types the details in and if the details don't match what is on file an error message is returned and displayed. This works! If the details are correct then a different html file is shown to the user (network.html) This is the bit I can't get working.

The current filesystem 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 right as I can access the index.html The code looks like this: (I have places a comment where I am trying to return the 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

Cheers

Chris

like image 713
Kraken18 Avatar asked Jun 30 '11 08:06

Kraken18


1 Answers

You have basically two options. If you want the user to visit /request and get that network.html content back, then 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'))

The other approach would be for the user to visit /request and, if authorized, be redirected to the content at another URL, perhaps /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')

Their browser will then make a second request for the new resource.

like image 96
fumanchu Avatar answered Nov 14 '22 23:11

fumanchu