Search code examples
socketshttppython-3.xtornado

Tornado on python3


My goal is to run http server on python3 using tornado (http://www.tornadoweb.org/). I started with running example code found on their webpage:

import tornado.httpserver
import tornado.ioloop

from tornado import httputil

def handle_request(request):
   message = "You requested %s\n" % request.uri
   request.connection.write_headers(
       httputil.ResponseStartLine('HTTP/1.1', 200, 'OK'),
       {"Content-Length": str(len(message))})
   request.connection.write(message)
   request.connection.finish()


http_server = tornado.httpserver.HTTPServer(handle_request)
http_server.listen(8080)
tornado.ioloop.IOLoop.instance().start()

This code throws following exception when receiving any http request:

    ERROR:tornado.application:Uncaught exception
Traceback (most recent call last):
  File "/usr/local/lib/python3.4/dist-packages/tornado/http1connection.py", line 234, in _read_message
    delegate.finish()
  File "/usr/local/lib/python3.4/dist-packages/tornado/httpserver.py", line 280, in finish
    self.server.request_callback(self.request)
  File "test.py", line 10, in handle_request
    {"Content-Length": str(len(message))})
  File "/usr/local/lib/python3.4/dist-packages/tornado/http1connection.py", line 367, in write_headers
    lines.extend([utf8(n) + b": " + utf8(v) for n, v in headers.get_all()])
AttributeError: 'dict' object has no attribute 'get_all'

Whats wrong ? This is sample code, so it should work and problem is somewhere in my enviroment ? I tried this both on latest Ubuntu and Win 7 and it gave me same error.

Thanks in advance


Solution

  • The documentation had a bug, fixed recently. For any version of Python, you can't pass a raw dict to write_headers. Instead you must wrap the dict in an HTTPHeaders object. For Python 3, the message must be a bytes instance. With both fixes:

    def handle_request(request):
       message = ("You requested %s\n" % request.uri).encode('ascii')
       request.connection.write_headers(
           httputil.ResponseStartLine('HTTP/1.1', 200, 'OK'),
           httputil.HTTPHeaders({"Content-Length": str(len(message))}))
       request.connection.write(message)
       request.connection.finish()