O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

Table of Contents

  1. Preface
    1. Conventions Used in This Book
    2. Using Code Examples
    3. Safari® Books Online
    4. How to Contact Us
    5. Acknowledgements
  2. 1. Introduction
    1. What Is Tornado?
      1. Getting Started with Tornado
      2. Community and Support
    2. Simple Web Services
      1. Hello Tornado
      2. String Service
      3. More About RequestHandlers
      4. Next Steps
  3. 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
      3. Next Steps with Templates
  4. 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
  5. 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
    4. MongoDB: Next Steps
  6. 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
  7. 6. Writing Secure Applications
    1. Cookie Vulnerabilities
      1. Cookie Forgery
      2. Secure Cookies
    2. Request Vulnerabilities
      1. Anatomy of a Cross-Site Request Forgery
      2. Defending Against Request Forgeries
      3. Using Tornado’s XSRF protection
    3. User Authentication
      1. Example: Welcome Back
      2. The authenticated Decorator
    4. Summing Up
  8. 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
  9. 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