tornado static file handler

11-23 842 views

# _*_ coding:UTF-8

import tornado.ioloop
import tornado.web
from protocol import typhoon_pb2
import utils
import os
import re

routeMapping = {
'^index$':                    "index.html",
'^index/$':                   "index.html",
'^login$':                    "index.html",
'^login/$':                   "index.html",
'^login.html$':               "index.html",
'^index.html$':               "index.html",
'^index/filesDetails/?.*$':   "index.html",
'^index/uploadFailed/?.*$':   "index.html",
'^index/uploading/?.*$':      "index.html",
'^index/downLoadFailed/?.*$': "index.html",
'^index/downLoading/?.*$':    "index.html",
'^index/uploaded/?.*$':       "index.html",
'^index/downLoaded/?.*$':     "index.html",

'^admin$':                 "admin/index.html",
'^admin/$':                "admin/index.html",
'^admin/login/?.*$':       "admin/index.html",
'^admin/dashboard/?.*$':   "admin/index.html",
'^admin/users/?.*$':       "admin/index.html",
'^admin/user_edit/?.*$':   "admin/index.html",
'^admin/admins/?.*$':      "admin/index.html",
'^admin/admin_edit/?.*$':  "admin/index.html",
'^admin/servers/?.*$':     "admin/index.html",
'^admin/server_edit/?.*$': "admin/index.html",
'^admin/assigns/?.*$':     "admin/index.html",
'^admin/assign_edit/?.*$': "admin/index.html"
}

class ApiHandler(tornado.web.RequestHandler):
    def post(self):
        data   = self.request.body

class StaticHandler(tornado.web.StaticFileHandler):
    def get_absolute_path(self, root, path):
        file = path
        if len(file) != 0 and file[0] == '/' :
            file = file[1:]

        for pattern in routeMapping :
            if re.match(pattern, file) :
                file = routeMapping[pattern]
                break

        if file == '' or file == '/':
            file = 'index.html'

        return super(StaticHandler, self).get_absolute_path(root, file)

def webserver(host = '127.0.0.1', port = 8090, map = None):
    static_path = os.path.abspath(utils.curdir() + '/static')
    app = tornado.web.Application([
        (r"/api", ApiHandler),
        (r'/(.*)', StaticHandler, {'path': static_path, 'default_filename': 'index.html'}),
    ])
    app.listen(port, address=host)
    try:
        tornado.ioloop.IOLoop.current().start()
    except socket.error as e:
        logger.error('Port ' + port + ' is occupied')
        logger.error(e.message)
        sys.exit(e.errno)

OpenCV-Python

OpenCV-Python OpenCV-Python Installing OpenCV from prebuilt binaries Below Python packages are to be downloaded and installed to their default lo...

阅读全文

Python 乱码解决

''.decode(sys.getfilesystemencoding())

阅读全文

python hideconsole

def hideConcole(): import ctypes kernel32 = ctypes.WinDLL('kernel32') user32 = ctypes.WinDLL('user32') SW_HIDE = 0 hWnd = k...

阅读全文

欢迎留言