sanic.dev
Open in
urlscan Pro
185.199.111.153
Public Scan
Submitted URL: http://sanic.dev/
Effective URL: https://sanic.dev/en/
Submission: On November 22 via api from US — Scanned from DE
Effective URL: https://sanic.dev/en/
Submission: On November 22 via api from US — Scanned from DE
Form analysis
0 forms found in the DOMText Content
Sanic Framework Home Docs Docs * USER GUIDE * General * Basics * Advanced * Best Practices * Running & Deploying * How to... * Latest Release Notes * OFFICIAL PLUGINS * Sanic Extensions * Sanic Testing * API DOCS * View API Docs (opens new window) * ORG DOCS * Feature Requests * Policies * S.C.O.P.E. Community Community * Forums (opens new window) * Discord (opens new window) * Twitter (opens new window) * Sponsorship (opens new window) Languages Languages * English * 简体中文 * 日本語 * 한국어 Help GitHub (opens new window) Home Docs Docs * USER GUIDE * General * Basics * Advanced * Best Practices * Running & Deploying * How to... * Latest Release Notes * OFFICIAL PLUGINS * Sanic Extensions * Sanic Testing * API DOCS * View API Docs (opens new window) * ORG DOCS * Feature Requests * Policies * S.C.O.P.E. Community Community * Forums (opens new window) * Discord (opens new window) * Twitter (opens new window) * Sponsorship (opens new window) Languages Languages * English * 简体中文 * 日本語 * 한국어 Help GitHub (opens new window) Current with version 23.6 Want more? sanicbook.com (opens new window) Check out our feature requests BUILD FAST. RUN FAST. Accelerate your web app development Get Started → SIMPLE AND LIGHTWEIGHT Intuitive API with smart defaults and no bloat allows you to get straight to work building your app. UNOPINIONATED AND FLEXIBLE Build the way you want to build without letting your tooling constrain you. PERFORMANT AND SCALABLE Built from the ground up with speed and scalability as a main concern. It is ready to power web applications big and small. PRODUCTION READY Out of the box, it comes bundled with a web server ready to power your web applications. TRUSTED BY MILLIONS Sanic is one of the overall most popular frameworks on PyPI, and the top async enabled framework COMMUNITY DRIVEN The project is maintained and run by the community for the community. # THE LIGHTNING-FAST ASYNCHRONOUS PYTHON WEB FRAMEWORK With the features and tools you'd expect. And some you wouldn't believe. * Production-grade * TLS server * Websockets * Static files * Lifecycle * Smart error handling * App Inspector * Extensible * Developer Experience After installing, Sanic has all the tools you need for a scalable, production-grade server—out of the box! Including full TLS support. from sanic import Sanic from sanic.response import text app = Sanic("MyHelloWorldApp") @app.get("/") async def hello_world(request): return text("Hello, world.") Copied! sanic path.to.server:app [2023-01-31 12:34:56 +0000] [999996] [INFO] Sanic v22.12.0 [2023-01-31 12:34:56 +0000] [999996] [INFO] Goin' Fast @ http://127.0.0.1:8000 [2023-01-31 12:34:56 +0000] [999996] [INFO] mode: production, single worker [2023-01-31 12:34:56 +0000] [999996] [INFO] server: sanic, HTTP/1.1 [2023-01-31 12:34:56 +0000] [999996] [INFO] python: 3.10.9 [2023-01-31 12:34:56 +0000] [999996] [INFO] platform: SomeOS-9.8.7 [2023-01-31 12:34:56 +0000] [999996] [INFO] packages: sanic-routing==22.8.0, sanic-testing==22.12.0, sanic-ext==22.12.0 [2023-01-31 12:34:56 +0000] [999997] [INFO] Sanic Extensions: [2023-01-31 12:34:56 +0000] [999997] [INFO] > injection [12 dependencies; 7 constants] [2023-01-31 12:34:56 +0000] [999997] [INFO] > openapi [http://127.0.0.1:8000/docs] [2023-01-31 12:34:56 +0000] [999997] [INFO] > http [2023-01-31 12:34:56 +0000] [999997] [INFO] > templating [jinja2==3.1.2] [2023-01-31 12:34:56 +0000] [999997] [INFO] Starting worker [999997] Copied! Running Sanic with TLS enabled is as simple as passing it the file paths... sanic path.to.server:app --cert=/path/to/bundle.crt --key=/path/to/privkey.pem Copied! ... or the a directory containing fullchain.pem and privkey.pem sanic path.to.server:app --tls=/path/to/certs Copied! Even better, while you are developing, let Sanic handle setting up local TLS certificates so you can access your site over TLS at https://localhost:8443 (opens new window) sanic path.to.server:app --dev --auto-tls Copied! Up and running with websockets in no time using the websockets (opens new window) package. from sanic import Request, Websocket @app.websocket("/feed") async def feed(request: Request, ws: Websocket): async for msg in ws: await ws.send(msg) Copied! Serving static files is of course intuitive and easy. Just name an endpoint and either a file or directory that should be served. app.static("/", "/path/to/index.html") app.static("/uploads/", "/path/to/uploads/") Copied! Moreover, serving a directory has two additional features: automatically serving an index, and automatically serving a file browser. Sanic can automatically serve index.html (or any other named file) as an index page in a directory or its subdirectories. app.static( "/uploads/", "/path/to/uploads/", index="index.html" ) Copied! And/or, setup Sanic to display a file browser. app.static( "/uploads/", "/path/to/uploads/", directory_view=True ) Copied! Beginning or ending a route with functionality is as simple as adding a decorator. @app.on_request async def add_key(request): request.ctx.foo = "bar" @app.on_response async def custom_banner(request, response): response.headers["X-Foo"] = request.ctx.foo Copied! Same with server events. @app.before_server_start async def setup_db(app): app.ctx.db_pool = await db_setup() @app.after_server_stop async def setup_db(app): await app.ctx.db_pool.shutdown() Copied! But, Sanic also allows you to tie into a bunch of built-in events (called signals), or create and dispatch your own. @app.signal("http.lifecycle.complete") # built-in async def my_signal_handler(conn_info): print("Connection has been closed") @app.signal("something.happened.ohmy") # custom async def my_signal_handler(): print("something happened") await app.dispatch("something.happened.ohmy") Copied! Raising errors will intuitively result in proper HTTP errors: raise sanic.exceptions.NotFound # Automatically responds with HTTP 404 Copied! Or, make your own: from sanic.exceptions import SanicException class TeapotError(SanicException): status_code = 418 message = "Sorry, I cannot brew coffee" raise TeapotError Copied! And, when an error does happen, Sanic's beautiful DEV mode error page will help you drill down to the bug quickly. Regardless, Sanic comes with an algorithm that attempts to respond with HTML, JSON, or text-based errors as appropriate. Don't worry, it is super easy to setup and customize your error handling to your exact needs. Check in on your live, running applications (whether local or remote). sanic inspect ┌─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐ │ Sanic │ │ Inspecting @ http://localhost:6457 │ ├───────────────────────┬─────────────────────────────────────────────────────────────────────────────────────────────┤ │ │ mode: production, single worker │ │ ▄███ █████ ██ │ server: unknown │ │ ██ │ python: 3.10.9 │ │ ▀███████ ███▄ │ platform: SomeOS-9.8.7 │ ██ │ packages: sanic==22.12.0, sanic-routing==22.8.0, sanic-testing==22.12.0, sanic-ext==22.12.0 │ │ ████ ████████▀ │ │ │ │ │ │ Build Fast. Run Fast. │ │ └───────────────────────┴─────────────────────────────────────────────────────────────────────────────────────────────┘ Sanic-Main pid: 999996 Sanic-Server-0-0 server: True state: ACKED pid: 999997 start_at: 2023-01-31T12:34:56.00000+00:00 starts: 1 Sanic-Inspector-0 server: False state: STARTED pid: 999998 start_at: 2023-01-31T12:34:56.00000+00:00 starts: 1 Copied! And, issue commands like reload, shutdown, scale... sanic inspect scale 4 Copied! ... or even create your own! sanic inspect migrations Copied! In addition to the tools that Sanic comes with, the officially supported Sanic Extensions provides lots of extra goodies to make development easier. * CORS protection * Template rendering with Jinja * Dependency injection into route handlers * OpenAPI documentation with Redoc and/or Swagger * Predefined, endpoint-specific response serializers * Request query arguments and body input validation * Auto create HEAD, OPTIONS, and TRACE endpoints * Live health monitor Sanic is built for building. From the moment it is installed, Sanic includes helpful tools to help the developer get their job done. * One server - Develop locally in DEV mode on the same server that will run your PRODUCTION application * Auto reload - Reload running applications every time you save a Python fil, but also auto-reload on any arbitrary directory like HTML template directories * Debuggin tools - Super helpful (and beautiful) error pages that help you traverse the trace stack easily * Auto TLS - Running a localhost website with https can be difficult, Sanic makes it easy * Streamlined testing - Built-in testing capabilities, making it easier for developers to create and run tests, ensuring the quality and reliability of their services * Modern Python - Thoughtful use of type hints to help the developer IDE experience MIT Licensed Copyright © 2018-present Sanic Community Organization ~ Made with ❤️ and ☕️ ~