Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Python Web Programming with Tornado
petercour
petercour

Posted on

     

Python Web Programming with Tornado

There are many modules to create web apps. Django and Flask are the most popular ones. But there are others, like Tornado.

How does Tornado work? It's actually quite simple. A single one page app is:

#!/usr/bin/python3# -*- coding: utf-8 -*-import tornado.ioloop   import tornado.webclass HomeHandler(tornado.web.RequestHandler):     def get(self):        self.write("hello, world")app = tornado.web.Application([ (r"/", HomeHandler), ])app.listen(8000)tornado.ioloop.IOLoop.instance().start()

Open localhost port 8000 on your web browser to see the hello world message. The script itself does not output anything in the terminal.

You can create different routes when initializing the tornado.Web.Application() call. Each route is a class.

#!/usr/bin/python3# -*- coding: utf-8 -*-import tornado.ioloop import tornado.webclass HomeHandler(tornado.web.RequestHandler):     def get(self):        self.write("hello, world")class AboutHandler(tornado.web.RequestHandler):     def get(self):        self.write("Mr President")app = tornado.web.Application([    (r"/", HomeHandler),     (r"/about", AboutHandler)])app.listen(8000)tornado.ioloop.IOLoop.instance().start()

Then you can open the / route or the /about route.

Related links:

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

  • Joined

More frompetercour

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp