Search code examples
pythonutf-8servertornado

tornado: 'utf-8' codec can't decode byte


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 can't access all of resources:
enter image description here

At the side of server, I get many errors as below:

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xa8 in position 14: invalid start byte

I think it's because there are some Chinese words in the frontend codes.

How to solve this issue?

Btw, I'm working on a remote CentOS with command line and there is no Desktop.


Solution

  • Have you tried adding

    # -*- coding: UTF-8 -*-
    

    to the top of your script? Not sure that would fix it, but it wouldn't hurt trying