Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 静态文件的CherryPy访问限制_Python_Cherrypy - Fatal编程技术网

Python 静态文件的CherryPy访问限制

Python 静态文件的CherryPy访问限制,python,cherrypy,Python,Cherrypy,我有一个CherryPy服务器,它将一些静态HTML/JS/等文件分发到/foosball,并通过RESTAPI将一些JSON分发到/ import cherrypy config = { 'global': { 'server.socket_host': '0.0.0.0', 'server.socket_port': # my port here, 'tools.json_in.on': True }, '/foosb

我有一个CherryPy服务器,它将一些静态HTML/JS/等文件分发到/foosball,并通过RESTAPI将一些JSON分发到/

import cherrypy

config = {
    'global': {
        'server.socket_host': '0.0.0.0',
        'server.socket_port': # my port here,
        'tools.json_in.on': True
    },
    '/foosball': {
        'tools.staticdir.on': True,
        'tools.staticdir.root': '/var/www/html',
        'tools.staticdir.dir': 'foosball',
        'tools.staticdir.index': 'index.html'
    }
}

@cherrypy.popargs('player_id')
class RESTClient_Player(object):
    # stuff

class RESTClient_Game(object):
    # stuff

class RESTClient:
    players = RESTClient_Player()
    games = RESTClient_Game()

    @cherrypy.expose
    def index(self):
        http_method = getattr(self, cherrypy.request.method)
        return (http_method)()

cherrypy.quickstart(RESTClient(), '/', config)
我还想让这些页面受到基本访问限制方案的保护,所以我一直在研究

问题是,文档旨在验证非静态页面,这类页面由
def
语句明确声明。我尝试将此文档改编为/foosball中的文件,但没有成功/foosball总是在没有任何身份验证请求的情况下加载

我可以添加什么来给静态文件一些访问限制功能

谢谢


编辑:我被指向了身份验证工具。通过下面的配置块,我可以通过登录屏幕锁定REST API部分,但是/foosball中的所有静态文件仍然可以公开访问:

def check_login_and_password(login, password):
    cherrypy.log(login)
    cherrypy.log(password)

    return

config = {
    'global': {
        'server.socket_host': '0.0.0.0',
        'server.socket_port': # my port here,
        'tools.json_in.on': True,
        'tools.sessions.on': True,
        'tools.session_auth.on': True,
        'tools.session_auth.check_username_and_password': check_login_and_password
    },
    '/foosball': {
        'tools.staticdir.on': True,
        'tools.staticdir.root': '/var/www/html',
        'tools.staticdir.dir': 'foosball',
        'tools.staticdir.index': 'index.html',
        'tools.sessions.on': True,
        'tools.session_auth.on': True,
        'tools.session_auth.check_username_and_password': check_login_and_password
    }
}
您可以在类中创建一个返回静态文件的函数,而不是在配置中使用“staticdir”。如果这样做,就可以围绕函数包装身份验证

import cherrypy
from cherrypy.lib.static import serve_file
import os

class Hello(object):
    @cherrypy.expose
    def index(self):
        return "Hello World"

    @cherrypy.expose
    def static(self, page):
        return serve_file(os.path.join(current_dir, 'static', page), content_type='text/html')


if __name__ == '__main__':
    current_dir = os.path.dirname(os.path.abspath(__file__))
    cherrypy.quickstart(Hello())

试试工具?谢谢,我不知道。请参阅上面的编辑以了解我的经验。文件必须来自浏览器缓存,请尝试Ctrl+Refresh。我刚刚尝试过,auth工具确实对我有用,尽管它不够聪明,无法在登录表单显示中将内容类型从图像更改为html。希望它是可配置的。之前发布的缓存不是罪魁祸首(我花掉了一周的浏览器缓存),但当我在Internet Explorer(我从未使用过的浏览器)中点击相同的URL时,我得到了正确的登录页面。很奇怪。但我想我现在就在我想去的地方,只要我能弄清楚如何正确地清除用户的缓存。顺便说一句,@jwalker,如果你把你的评论作为一个答案转发给我,我会相信你。对于缓存控制,请尝试
expires
工具。我不确定我的建议是否符合答案,尤其是因为我们还不知道这是否真的可行。