Search code examples
htmltwitter-bootstrapcssservertornado

tornado: why do I get http 500 error


i'm using python tornado to build a simple web server. Here is the code of tornado:

import json
import tornado.httpserver
import tornado.ioloop
import tornado.options
import tornado.web

from tornado.options import define, options
define("port", default=80, help="run on the given port", type=int)

class IndexHandler(tornado.web.RequestHandler):
        def get(self, param):
            print("\n\nthis is a get request from indexhandler:")
            if param:
                print("param is NOT null")
                self.render(r"frontend/" + param)
            else:
                print("param is null")
                self.render(r"frontend/index.html")

if __name__ == "__main__":
    tornado.options.parse_command_line()
    app = tornado.web.Application(handlers=[(r"/(.*)", IndexHandler)])
    http_server = tornado.httpserver.HTTPServer(app)
    http_server.listen(options.port)
    tornado.ioloop.IOLoop.instance().start()

All of the frontend codes are in the directory /frontend so I used a simple regex (.*) to permit user to access all of resources in /frontend, such as js files and css files.

However, when I try to visit my website, I get some 304 errors at the server:

[I 170501 14:31:59 web:2063] 200 GET /html/country.html
[I 170501 14:31:59 web:2063] 304 GET /css/bootstrap.min.css
[I 170501 14:31:59 web:2063] 304 GET /css/reset.css
[I 170501 14:31:59 web:2063] 304 GET /css/icon/iconfont.css
[I 170501 14:31:59 web:2063] 304 GET /css/country.css
[I 170501 14:31:59 web:2063] 304 GET /css/common.css

UPDATE
I have another issue: error 500

In a word, I have some 304 and some 500. All of 500 are like this:

[E 170501 22:53:19 web:1590] Uncaught exception GET /images/main-img1.jpg (X.X.X.X)
    HTTPServerRequest(protocol='http', host='X.X.X.X', method='GET', uri='/images/main-img1.jpg', version='HTTP/1.1', remote_ip='X.X.X.X', headers={'Accept-Encoding': 'gzip, deflate, sdch', 'Accept-Language': 'fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4', 'Accept': 'image/webp,image/*,*/*;q=0.8', 'Host': 'X.X.X.X', 'Referer': 'http://X.X.X.X/html/country.html', 'Connection': 'keep-alive', 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36'})
    Traceback (most recent call last):
      File "/usr/local/lib/python3.5/site-packages/tornado/web.py", line 1509, in _execute
        result = method(*self.path_args, **self.path_kwargs)
      File "tmp.py", line 20, in get
        self.render("frontend/" + param)
      File "/usr/local/lib/python3.5/site-packages/tornado/web.py", line 724, in render
        html = self.render_string(template_name, **kwargs)
      File "/usr/local/lib/python3.5/site-packages/tornado/web.py", line 862, in render_string
        t = loader.load(template_name)
      File "/usr/local/lib/python3.5/site-packages/tornado/template.py", line 427, in load
        self.templates[name] = self._create_template(name)
      File "/usr/local/lib/python3.5/site-packages/tornado/template.py", line 455, in _create_template
        template = Template(f.read(), name=name, loader=self)
      File "/usr/local/lib/python3.5/site-packages/tornado/template.py", line 304, in __init__
        reader = _TemplateReader(name, escape.native_str(template_string),
      File "/usr/local/lib/python3.5/site-packages/tornado/escape.py", line 218, in to_unicode
        return value.decode("utf-8")
    UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte
[E 170501 22:53:19 web:2063] 500 GET /images/main-img1.jpg (X.X.X.X) 2.07ms

Solution

  • HTTP 304 is "Not Modified". It's not an error, it's an optimization. Tornado (like most web servers) tells your browser each static file's last-modified-date and a checksum of its contents (its "ETag" in HTTP terms). When the browser requests the file again, the browser tells Tornado which last-modified date and ETag it has in the browser's cached copy; Tornado compares those to its own and, if they haven't changed, just tells the browser "304 Not Modified". Thus, the browser knows it can use its cached copy and doesn't have to re-download the original.

    The HTTP 500 is the actual problem. You have some characters in your template file that are not valid UTF-8. Apparently the very first character is not valid UTF-8, based on the "position 0" in the error message.