Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

404 GET (Trying to use Tornado Server to serve static files):

I have been trying to use the Tornado Web Server to serve static files however when I run the server I get 404 errors for the static files:

[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

This is the code block that I am using to start the server:

#!/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()

I have tried to add a handler for the static file, but I keep getting the 404 error. Thanks for any solutions.

like image 410
Eddie Avatar asked Dec 09 '25 20:12

Eddie


1 Answers

The first matching handler is used, which in this case is the FallbackHandler (which matches everything: ".*". Move this to after the /static/(.*) rule.

like image 62
Ben Darnell Avatar answered Dec 11 '25 13:12

Ben Darnell



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!