Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you run the Tornado web server locally?

Tags:

python

tornado

Is it possible to run Tornado such that it listens to a local port (e.g. localhost:8000). I can't seem to find any documentation explaining how to do this.

like image 430
Michael Avatar asked Jun 19 '12 05:06

Michael


People also ask

Is Tornado a webserver?

Tornado is a scalable, non-blocking web server and web application framework written in Python. It was developed for use by FriendFeed; the company was acquired by Facebook in 2009 and Tornado was open-sourced soon after.

How does Tornado Python work?

Tornado is a Python web framework and asynchronous network library, originally developed at FriendFreed. Tornado uses non-blocking network-io. Due to this, it can handle thousands of active server connections. It is a saviour for applications where long polling and a large number of active connections are maintained.

What is Tornado module in Python?

Tornado is a Python web framework and asynchronous networking library, originally developed at FriendFeed.

What is Tornado web application?

A Tornado web application generally consists of one or more RequestHandler subclasses, an Application object which routes incoming requests to handlers, and a main() function to start the server.


3 Answers

Add an address argument to Application.listen() or HTTPServer.listen().

It's documented here (Application.listen) and here (TCPServer.listen).

For example:

application = tornado.web.Application([
    (r'/blah', BlahHandler),
    ], **settings)

# Create an HTTP server listening on localhost, port 8080.
http_server = tornado.httpserver.HTTPServer(application)
http_server.listen(8080, address='127.0.0.1')
like image 139
Rod Hyde Avatar answered Sep 29 '22 20:09

Rod Hyde


In the documetaion they mention to run on the specific port like

import tornado.ioloop
import tornado.web

class MainHandler(tornado.web.RequestHandler):
    def get(self):
        self.write("Hello, world")

application = tornado.web.Application([
    (r"/", MainHandler),
])

if __name__ == "__main__":
    application.listen(8000)
    tornado.ioloop.IOLoop.instance().start()

You will get more help from http://www.tornadoweb.org/documentation/overview.html and http://www.tornadoweb.org/documentation/index.html

like image 32
Nilesh Avatar answered Sep 29 '22 21:09

Nilesh


Once you've defined an application (like in the other answers) in a file (for example server.py), you simply save and run that file.

python server.py

like image 42
Adam Loving Avatar answered Sep 29 '22 20:09

Adam Loving