Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Python Web Development with Sanic

You're reading from   Python Web Development with Sanic An in-depth guide for Python web developers to improve the speed and scalability of web applications

Arrow left icon
Product type Paperback
Published in Mar 2022
Publisher Packt
ISBN-13 9781801814416
Length 504 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Authors (2):
Arrow left icon
Stephen Sadowski Stephen Sadowski
Author Profile Icon Stephen Sadowski
Stephen Sadowski
Adam Hopkins Adam Hopkins
Author Profile Icon Adam Hopkins
Adam Hopkins
Arrow right icon
View More author details
Toc

Table of Contents (16) Chapters Close

Preface 1. Part 1:Getting Started with Sanic
2. Chapter 1: Introduction to Sanic and Async Frameworks FREE CHAPTER 3. Chapter 2: Organizing a Project 4. Part 2:Hands-On Sanic
5. Chapter 3: Routing and Intaking HTTP Requests 6. Chapter 4: Ingesting HTTP Data 7. Chapter 5: Building Response Handlers 8. Chapter 6: Operating Outside the Response Handler 9. Chapter 7: Dealing with Security Concerns 10. Chapter 8: Running a Sanic Server 11. Part 3:Putting It All together
12. Chapter 9: Best Practices to Improve Your Web Applications 13. Chapter 10: Implementing Common Use Cases with Sanic 14. Chapter 11: A Complete Real-World Example 15. Other Books You May Enjoy

Chapter 1: Introduction to Sanic and Async Frameworks

There should be one—and preferably only one—obvious way to do it.

– Tim Peters, The Zen of Python

Too often, this maxim of Python is taken to mean that there must be only one way to do something. Any Python web developer can simply look at the number of web frameworks that exist and tell you that the choice is not so simple. There are dozens of web frameworks on PyPI, and within the ecosystem of any single framework, you will find even more options to solve a single problem. Go ahead and type authentication into the search bar at https://pypi.org. Looking at the number of results, stating that there is only "[one] obvious way to do it" does not seem so obvious. Maybe this sentence needs to be changed. Perhaps it could read, "There should be one … obvious way for you to do it." Why? Because adding to the context that we are talking about your specific application brings us to the next level.

This is Sanic, and this is the goal of this book.

What may be obvious for someone building a stock portfolio tracker will not be obvious to someone building a streaming media player. So, to figure out what the obvious solution is, we must first understand the problem. And to understand the problem, we must be hyper-aware of our specific use case.

When we're trying to find a solution to a problem, many other tools and frameworks respond by saying: here is how you should do it. Do you want to read data from your web request? Here's how to validate it. Do you need cross-site request forgery (CSRF) protection? Here's the snippet you need to add. This approach fails to make you a better developer and fails to find the optimal solution for your use case.

Why should I validate my data this way? Why do I need this snippet to protect myself? Because someone else decided for you. You cannot answer these questions. All you know is that the framework documentation—or some blog on the internet—told you to do this, so you did it.

And this is why Sanic—and indeed, this book—takes a different approach. By the end of this book, we want you to know how to spot peculiar use cases, as well as how to bend the tooling to meet your needs. You should be able to think through different types of implementations and select one that is most meaningful for your needs. This will be the obvious solution.

Sanic prides itself on being unopinionated. That is not to say that the maintainers of the project do not have strong opinions. I welcome you to engage me or anyone in the community in a discussion about proxy forwarding, deployment strategies, authentication schemes, and more. You will certainly find passionate opinions. By "unopinionated," we mean to say that Sanic's job is to take care of the plumbing so that all you need to do is build the logic. The decision of how to tackle problems is not the domain of the framework.

You will find that Sanic developers are mostly keen to find solutions that are hyper-focused on solving the particular challenges that they face. Developers use Sanic because it is fast and simple. However, you will also find that using Sanic means the obvious solution to a problem is not based upon Sanic but upon your unique application requirements.

The other side of the story is that sometimes, your use case doesn't need a hyper-focused solution. This is also fine. For this reason, you will find several plugins (many of which are supported by active members of the Sanic core developer team) or off-the-shelf solutions. We wholly support your adoption of them and their patterns. Throughout this book, our examples will steer away from implementations that require plugins. However, where there are popular solutions that include plugins, we will also point them out to you for reference.

Our goal in this book is to help you learn how to identify your unique application requirements and match them with the tools at your disposal. This will help make your applications better and make you a better developer.

In this chapter, we will begin by building the foundational understanding that's needed to read this book. To do this, we will cover the following topics:

  • What is Sanic?
  • Leveling up
  • Framework versus server
  • Why we use Sanic—build fast, run fast
You have been reading a chapter from
Python Web Development with Sanic
Published in: Mar 2022
Publisher: Packt
ISBN-13: 9781801814416
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime