Cherrypy 中的静态 html 文件

发布于 2024-11-17 23:50:04 字数 1787 浏览 0 评论 0原文

我对cherrypy中的基本概念有疑问,但到目前为止我还无法找到有关如何执行此操作的教程或示例(我是Cherrypy新手,请温柔点)。

问题。 (这是一个测试片段,因此代码中缺乏强大的身份验证和会话)

用户转到index.html页面,该页面是一个登录页面,输入详细信息,如果详细信息与文件中的内容不匹配,则会出现错误返回并显示消息。这有效! 如果详细信息正确,则会向用户显示不同的 html 文件(network.html),这是我无法工作的部分。

当前的文件系统如下所示:-

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

文件的布局似乎是正确的,因为我可以访问index.html 代码如下所示:(我在尝试返回新页面的地方发表了评论)

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()

在这件事上的任何帮助或指导将不胜感激

干杯

克里斯

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓝色星空 2024-11-24 23:50:04

你基本上有两个选择。如果您希望用户访问 /request 并获取 network.html 内容,则只需返回它即可:

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'))

另一种方法是让用户访问 /request 并获取如果获得授权,将被重定向到另一个 URL(可能是 /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')

然后他们的浏览器将发出对新资源的第二次请求。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文