簡體   English   中英

防止直接文件訪問龍卷風中的服務器文件

[英]Prevent direct file access to server files in tornado

我在龍卷風網絡服務器上使用python。 該應用程序運行正常,但我找不到阻止用戶直接通過url訪問服務器文件的方法。 例如,我在服務器中有以下文件:

program.py
的index.html
main.html中

我想防止用戶直接通過網址訪問上述服務器文件
例如:localhost:8080 / program.py或/index.html

我只希望他們訪問localhost:8080 /或/ home

提前致謝

from ws4py.client.tornadoclient import TornadoWebSocketClient
import tornado.ioloop
import tornado.web
import tornado.websocket
import tornado.template

SETTING_CLIENT_LISTEN_PORT = 8080
class MainHandler(tornado.web.RequestHandler):

    def get(self):
        try:
            loader = tornado.template.Loader(".")
            self.write(loader.load("index.html").generate())
        except Exception as e:
            print("exception occured", e)

class CWSHandler(tornado.websocket.WebSocketHandler):
    global  waiters

    def open(self):
        print('###FUNCTION CWSHandler.open(self) start')

    def on_close(self):
        print('###FUNCTION CWSHandler.open(self) close')

    def on_message(self, message):
        print('###FUNCTION CWSHandler.on_message msg:', message)

settings = {
    "cookie_secret": "bZJc2sWbQLKos6GkHn/VB9oXwQt8S0R0kRvJ5/xJ89E=",
    "login_url": "/",
}

application = tornado.web.Application(handlers=[
    (r'/', MainHandler),    
    (r'/cws', CWSHandler),


    (r"/(.*)", tornado.web.StaticFileHandler,{'path':'./'})
    ], cookie_secret="bZJc2sWbQLKos6GkHn/VB9oXwQt8S0R0kRvJ5/xJ89E=")

if __name__ == "__main__":
    server = tornado.httpserver.HTTPServer(application)
    server.listen(SETTING_CLIENT_LISTEN_PORT)

    try:
        tornado.ioloop.IOLoop.instance().start()
        server.stop()
    except KeyboardInterrupt:
        print("Keyboard interupt")
        pass
    finally:
        server.stop()
        tornado.ioloop.IOLoop.instance().stop()

問題出在您的網址,特別是:

(r"/(.*)", tornado.web.StaticFileHandler,{'path':'./'})

您已經將r'/(.*)'映射到{'path': './'} ,這是您的項目目錄。 因此,如果一個請求像localhost:8080/program.py一樣/(.*) ,它將與此- localhost:8080/program.py /(.*)匹配,然后龍卷風將在您的項目目錄中查找名為program.py的文件。 如果在此找到它,它將提供該文件。

您應該將所有靜態文件保存在項目目錄中一個單獨的目錄中,該目錄稱為static (盡管您可以命名為任意名稱)。 然后使用所需的URL映射此目錄。

例:

(r"/(.*)", tornado.web.StaticFileHandler,{'path': 'static'})

或者更好的是,在/static/ url下而不是- .*下提供該目錄。

(r"/static/(.*)", tornado.web.StaticFileHandler,{'path': 'static'})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM