Michael
Michael

Reputation: 2051

How do you run the Tornado web server locally?

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.

Upvotes: 9

Views: 20849

Answers (4)

Adam Loving
Adam Loving

Reputation: 443

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

Upvotes: 2

R Hyde
R Hyde

Reputation: 10409

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')

Upvotes: 24

Nikolay Fominyh
Nikolay Fominyh

Reputation: 9246

If you want to daemonize tornado - use supervisord. If you want to access tornado on address like http://mylocal.dev/ - you should look at nginx and use it like reverse proxy. And on specific port it can be binded like in Lafada's answer.

Upvotes: 0

Neel
Neel

Reputation: 21243

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

Upvotes: 2

Related Questions