Voglio sapere se c'è un modo migliore per gestire il mio file index.html con Tornado.
c'è un modo migliore per gestire index.html con Tornado?
Io uso StaticFileHandler per tutte le richieste e uso un MainHandler specifico per gestire la mia richiesta principale. Se io uso solo StaticFileHandler ho ottenuto un 403: errore di Forbidden
GET http://localhost:9000/
WARNING:root:403 GET/(127.0.0.1): is not a file
qui come faccio ora:
import os
import tornado.ioloop
import tornado.web
from tornado import web
__author__ = 'gvincent'
root = os.path.dirname(__file__)
port = 9999
class MainHandler(tornado.web.RequestHandler):
def get(self):
try:
with open(os.path.join(root, 'index.html')) as f:
self.write(f.read())
except IOError as e:
self.write("404: Not Found")
application = tornado.web.Application([
(r"/", MainHandler),
(r"/(.*)", web.StaticFileHandler, dict(path=root)),
])
if __name__ == '__main__':
application.listen(port)
tornado.ioloop.IOLoop.instance().start()
Sarebbe meglio sostituire 'r"/"' e 'r" /(.*) "' con 'r"/$ "' e 'r" /(.*)$ "'. – akaRem