Introduction to Tornado

Book description

Walk through the basics of Tornado, the high-performance web server known for its speed, simplicity, and scalability on projects large and small. With this hands-on guide, you’ll learn how to use Tornado’s acclaimed features by working with several example applications. You also get best practices for using Tornado in the real world.

Are you interested in creating a scalable social application, real-time analytics engine, or RESTful API—all with the power and simplicity of Python? This book shows you why Tornado is fantastic choice for writing powerful applications that are simple to create, extend, and deploy.

  • Learn how to use Tornado’s lightweight and flexible templating language
  • Extend templates to repurpose headers, footers, layout grids, and other content
  • Use persistent storage like MongoDB to store, serve, and edit dynamic content
  • Explore Tornado’s ability to make asynchronous web requests
  • Secure your application against cookie and request vulnerabilities
  • Authenticate with external services, using Tornado’s auth module
  • Adopt deployment strategies that help harden your application and increase request throughput

Publisher resources

View/Submit Errata

Table of contents

  1. Introduction to Tornado
  2. SPECIAL OFFER: Upgrade this ebook with O’Reilly
  3. Preface
    1. Conventions Used in This Book
    2. Using Code Examples
    3. Safari® Books Online
    4. How to Contact Us
    5. Acknowledgements
  4. 1. Introduction
    1. What Is Tornado?
      1. Getting Started with Tornado
      2. Community and Support
    2. Simple Web Services
      1. Hello Tornado
        1. The handlers Parameter
        2. Specifying paths with regular expressions
      2. String Service
      3. More About RequestHandlers
        1. HTTP methods
        2. HTTP status codes
      4. Next Steps
  5. 2. Forms and Templates
    1. Simple Example: Poem Maker Pro
      1. Rendering Templates
      2. Interpolation
    2. Template Syntax
      1. Interpolating Expressions
      2. Control Flow Statements
      3. Using Functions Inside Templates
    3. Complete Example: The Alpha Munger
      1. How It Works
      2. Serving Static Files
        1. Setting the static_path
        2. Generating static URLs with static_url
      3. Next Steps with Templates
  6. 3. Extending Templates
    1. Blocks and Substitutions
      1. Basics of Blocks
      2. Templates in Practice: Burt’s Books
      3. Autoescaping
    2. UI Modules
      1. Basic Module Usage
      2. Modules in Depth
      3. Embedding JavaScript and CSS
    3. Summing Up
  7. 4. Databases
    1. Basic MongoDB Operations with PyMongo
      1. Establishing a Connection
      2. Dealing with Documents
      3. MongoDB Documents and JSON
    2. A Simple Persistent Web Service
      1. A Read-Only Dictionary
      2. Writing the Dictionary
    3. Burt’s Books
      1. Reading Books (From the Database)
      2. Editing and Adding Books
        1. Rendering the edit form
        2. Retrieving book information from the database
        3. Saving to the database
    4. MongoDB: Next Steps
  8. 5. Asynchronous Web Services
    1. Asynchronous Web Requests
      1. Starting Synchronous
      2. The Trouble with Blocking
      3. Basic Asynchronous Calls
      4. The asynchronous Decorator and the finish Method
      5. Asynchronous Generators
      6. Summary of Asynchronous Operations
    2. Long Polling with Tornado
      1. The Benefits of Long Polling
      2. Example: Live Inventory Reporting
      3. The Downsides of Long Polling
    3. WebSockets with Tornado
      1. Tornado’s WebSocket Module
      2. Example: Live Inventory with WebSockets
      3. The Future of WebSockets
  9. 6. Writing Secure Applications
    1. Cookie Vulnerabilities
      1. Cookie Forgery
      2. Secure Cookies
        1. Using Secure Cookies
        2. HTTP-Only and SSL Cookies
    2. Request Vulnerabilities
      1. Anatomy of a Cross-Site Request Forgery
      2. Defending Against Request Forgeries
      3. Using Tornado’s XSRF protection
        1. XSRF Tokens and AJAX Requests
    3. User Authentication
      1. Example: Welcome Back
      2. The authenticated Decorator
        1. The current_user property
        2. The login_url setting
    4. Summing up
  10. 7. Authenticating with External Services
    1. The Tornado auth Module
      1. The Authorization Workflow
      2. Asynchronous Requests
    2. Example: Sign in With Twitter
    3. Example: Facebook Authentication and the Graph API
  11. 8. Deploying Tornado
    1. Reasons for Running Multiple Tornado Instances
    2. Using Nginx as a Reverse Proxy
      1. Basic Nginx Configuration
      2. SSL Decryption with Nginx
    3. Using Supervisor to Manage Tornado Processes
  12. About the Authors
  13. SPECIAL OFFER: Upgrade this ebook with O’Reilly

Product information

  • Title: Introduction to Tornado
  • Author(s): Michael Dory, Allison Parrish, Brendan Berg
  • Release date: March 2012
  • Publisher(s): O'Reilly Media, Inc.
  • ISBN: 9781449333973