Helion


Szczegóły ebooka

Python Web Development with Sanic

Python Web Development with Sanic


Today's developers need something more powerful and customizable when it comes to web app development. They require effective tools to build something unique to meet their specific needs, and not simply glue a bunch of things together built by others. This is where Sanic comes into the picture. Built to be unopinionated and scalable, Sanic is a next-generation Python framework and server tuned for high performance.

This Sanic guide starts by helping you understand Sanic's purpose, significance, and use cases. You'll learn how to spot different issues when building web applications, and how to choose, create, and adapt the right solution to meet your requirements. As you progress, you'll understand how to use listeners, middleware, and background tasks to customize your application. The book will also take you through real-world examples, so you will walk away with practical knowledge and not just code snippets.

By the end of this web development book, you'll have gained the knowledge you need to design, build, and deploy high-performance, scalable, and maintainable web applications with the Sanic framework.

  • Python Web Development with Sanic
  • Contributors
  • About the author
  • About the reviewers
  • Preface
    • Who this book is for
    • What this book covers
    • To get the most out of this book
    • Download the example code files
    • Download the color images
    • Conventions used
    • Get in touch
    • Share Your Thoughts
  • Part 1:Getting Started with Sanic
  • Chapter 1: Introduction to Sanic and Async Frameworks
    • Technical requirements
    • What is Sanic?
    • Leveling up
    • Framework versus server
      • Web server
      • Web framework
    • Why we use Sanicbuild fast, run fast
      • Simple and lightweight
      • Unopinionated and flexible
      • Performant and scalable
      • Production-ready
      • Trusted by millions
      • Community-driven
      • What drives code decisions?
    • Summary
  • Chapter 2: Organizing a Project
    • Technical requirements
    • Setting up an environment and directory
      • Environment
      • Sanic CLI
      • Directory structure
    • Using blueprints effectively
      • Blueprint registration
      • Blueprint versioning
      • Grouping blueprints
    • Wiring it all up
      • Controlled imports
      • Factory pattern
      • Autodiscovery
    • Running our application
    • Summary
  • Part 2:Hands-On Sanic
  • Chapter 3: Routing and Intaking HTTP Requests
    • Technical requirements
    • Understanding HTTP methods
      • Using HTTP methods on route handlers
      • Advanced method routing
      • Method safety and request body
      • RESTful API design
      • Simplifying your endpoints with CBVs
      • Blanket support for OPTIONS and HEAD
    • Paths, slashes, and why they matter
      • Strict slashes
      • Extracting information from the path
    • Advanced path parameters
      • Custom parameter matching
      • Modifying matched parameter values
    • API versioning
      • Should all of my routes bump versions?
      • Version prefixing
    • Virtual hosts
    • Serving static content
      • Serving static content from Sanic
      • Serving static content with Nginx
      • Streaming static content
    • Summary
  • Chapter 4: Ingesting HTTP Data
    • Technical requirements
    • Reading cookies and headers
      • Headers are flexible
      • Authentication headers
      • Context headers
      • Sanic extracts header data for us
      • Headers as multi-dict
      • Getting information from cookies (yum!)
    • Reading forms, query arguments, files, JSON, and more
      • Query arguments
      • Forms and files
      • Consuming JSON data
    • Getting streaming data
    • Validating data
      • Step 1getting started and making a decorator
      • Step 2reading the handler signature
      • Step 3modeling
      • Step 4model hydration
      • Step 5performing validations
      • Taking it to the next level with third-party packages
    • Summary
  • Chapter 5: Building Response Handlers
    • Technical requirements
    • Examining the HTTP response structure
      • The HTTP response status
      • Response groupings
      • A response through exceptions
      • Custom status
      • Headers
      • The response body
    • Rendering HTML content
      • Delivering HTML files
      • Basic templating
      • Using a templating engine
    • Serializing JSON content
      • Choosing a serializer
      • Serializing custom objects
      • Best practices
    • Streaming data
      • File streaming
    • Server-sent events for push communication
      • Starting with the basics
      • Building some SSE objects
    • Websockets for two-way communication
    • Setting response headers and cookies
      • Responding with a request ID
      • Setting response cookies
    • Summary
  • Chapter 6: Operating Outside the Response Handler
    • Technical requirements
    • Making use of ctx
    • Altering requests and responses with middleware
      • Request middleware
      • Response middleware
      • Responding early (or late) with middleware
      • Middleware and streaming responses
    • Leveraging signals for intra-worker communication
      • Signal definitions
      • Using built-in signals
      • Custom signals
      • Waiting on events
    • Mastering HTTP connections
      • Keep-Alive within Sanic
      • Caching data per connection
      • Handling exceptions like a pro
    • Implementing proper exception handling
      • Bad exception messages
      • Misusing statuses
      • Responses through raising an exception
      • Fallback handling
      • Catching exceptions
    • Background task processing
      • Adding tasks to the loop
      • Integrating with an outside service
      • Designing an in-process task queue
    • Summary
  • Chapter 7: Dealing with Security Concerns
    • Technical requirements
    • Setting up an effective CORS policy
      • What is the security issue with ineffective CORS?
    • Protecting applications from CSRF
      • Solutions that do not work
      • Solutions that do work
      • Samesite cookies
    • Protecting your Sanic app with authentication
      • Using API keys
      • Understanding session-based versus non-session-based authentication
      • Using sessions
      • JSON Web Tokens (JWTs)
    • Summary
  • Chapter 8: Running a Sanic Server
    • Technical requirements
    • Handling the server life cycle
      • Server listeners
    • Configuring an application
      • What is the Sanic configuration object?
      • How can an applications configuration object be accessed?
      • How can the configuration object be set?
      • Some general rules about configuration
    • Running Sanic locally
      • How does running Sanic locally differ from production?
    • Deploying to production
      • Choosing the right server option
      • How to choose a deployment strategy?
    • Securing your application with TLS
      • Setting up TLS in Sanic
      • Getting and renewing a certificate from Let's Encrypt
    • Deployment examples
      • PaaS
      • Kubernetes (as-a-service)
    • Summary
  • Part 3:Putting It All together
  • Chapter 9: Best Practices to Improve Your Web Applications
    • Technical requirements
    • Implementing practical real-world exception handlers
      • Catching errors with middleware
      • Catching errors with signals
      • Catching the error and responding manually
      • Modifying ErrorHandler
    • Setting up a testable application
      • Getting started with sanic-testing
      • A more practical test client implementation
      • Using ReusableClient for testing
    • Gaining insight from logging and tracing
      • Types of Sanic loggers
      • Creating your own loggers, my first step in application development
      • Configuring logging
      • Adding color context
      • Adding some basic tracing with request IDs
      • Using X-Request-ID
    • Managing database connections
      • To ORM or not to ORM, that is the question
      • Creating a custom data access layer in Sanic
      • Connecting Sanic to Redis
    • Summary
  • Chapter 10: Implementing Common Use Cases with Sanic
    • Technical requirements
    • Synchronizing and scaling websocket feeds
    • Powering a progressive web application
      • Dealing with subdomains and CORS
      • Running a development server
    • Designing a GraphQL API
      • Why would I want to use GraphQL?
      • Adding GraphQL to Sanic
    • Building a Discord bot: running Sanic from another service
      • Building a simple Discord bot
      • Running the Discord bot from Sanic
    • Creating an HTTP to HTTPS proxy: nesting Sanic inside Sanic
    • Summary
  • Chapter 11: A Complete Real-World Example
    • Technical requirements
    • The process of building a web application
      • Step 1Define the functionality and workflow
      • Step 2Decide on the technology stack
      • Step 3Architect the data structures
      • Step 4Plan and build the user interface
      • Step 5Build the application infrastructure
      • Step 6Prototype the minimally viable backend features
      • Step 7Create continuous integration, deployment, and automation tools
      • Step 8Iterate, iterate, iterate
    • Highlighting select features of the Booktracker
      • Development environment
      • Creating a better factory pattern with setup functions
      • The data access layer
      • Authentication flow
    • Summary
    • Putting it all together
    • Why subscribe?
  • Other Books You May Enjoy
    • Packt is searching for authors like you
    • Share Your Thoughts

  • Tytuły: Python Web Development with Sanic
  • Autor: Adam Hopkins
  • Tytuł oryginału: Python Web Development with Sanic
  • ISBN Ebooka: 9781801816434, 9781801816434
  • Data wydania: 2022-03-28
  • Identyfikator pozycji: e_2t2l
  • Kategorie:
  • Wydawca: Packt Publishing