简体   繁体   中英

How do you run the Tornado web server locally?

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

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

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

一旦在文件(例如server.py)中定义了应用程序(如其他答案),就可以保存并运行该文件。

python server.py

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM