404 GET(尝试使用Tornado Server提供静态文件):

时间:2014-07-15 22:30:16

标签: django http python-2.7 tornado

我一直在尝试使用Tornado Web Server来提供静态文件,但是当我运行服务器时,静态文件出现了404错误:

[I 140715 17:16:50 wsgi:358] 200 GET /myapp/index/ (127.0.0.1) 99.01ms
[W 140715 17:16:50 wsgi:358] 404 GET /static/logo_small_white.png (127.0.0.1) 5.68ms
[I 140715 17:17:04 wsgi:358] 200 GET /myapp/index/ (127.0.0.1) 6.02ms
[W 140715 17:17:05 wsgi:358] 404 GET /static/logo_small_white.png (127.0.0.1) 5.05ms

这是我用来启动服务器的代码块:

#!/usr/bin/env python

# Run this with
# PYTHONPATH=. DJANGO_SETTINGS_MODULE=testsite.settings testsite/tornado_main.py
# Serves by default at
# http://localhost:8080/hello-tornado and
# http://localhost:8080/hello-django

from tornado.options import options, define, parse_command_line
import django.core.handlers.wsgi
import tornado.httpserver
import tornado.ioloop
import tornado.web
import tornado.wsgi
import os

define('port', type=int, default=8080)

class HelloHandler(tornado.web.RequestHandler):
  def get(self):
    self.write('Hello from tornado')

def main():
  settings = {
    "static_path": os.path.join(os.path.dirname(__file__), "static"),
    }
  parse_command_line()
  wsgi_app = tornado.wsgi.WSGIContainer(
    django.core.handlers.wsgi.WSGIHandler())
  tornado_app = tornado.web.Application(
    [
      ('/hello-tornado', HelloHandler),
      ('.*', tornado.web.FallbackHandler, dict(fallback=wsgi_app)),
      (r'/static/(.*)', tornado.web.StaticFileHandler, {'path': "/home/me/Downloads/javaAutoGraderBuilding/django-tornado-demo-master/testsite/static"}),
      ])
  server = tornado.httpserver.HTTPServer(tornado_app)
  server.listen(options.port)
  tornado.ioloop.IOLoop.instance().start()

if __name__ == '__main__':
  main()

我曾尝试为静态文件添加处理程序,但我一直收到404错误。感谢任何解决方案。

1 个答案:

答案 0 :(得分:5)

使用第一个匹配处理程序,在这种情况下是FallbackHandler(匹配所有内容:".*"。将其移至/static/(.*)规则之后。