Skip to main content

ASGIMiddlewareStaticFile

GitHub License PyPI - Version Python Version from PEP 621 TOML Pytest Workflow Status codecov Checked with mypy Code style: black PyPI - Downloads

ASGI Middleware for serving static file.

Why?

ASGIMiddlewareStaticFile is a solution when we need to distribute the whole project with static files in Docker; or when the deployment environment has very limited resources; or Internal network(Unable to reach CDN).

Features

  • Standard ASGI middleware implement
  • Async file IO
  • Support ETag, base on md5(file_size + last_modified)

Install

pip3 install -U ASGIMiddlewareStaticFile

Usage

Common

Prepare

pip3 install -U ASGIMiddlewareStaticFile
git clone https://github.com/rexzhang/asgi-middleware-static-file.git
cd asgi-middleware-static-file/example

Test with wget

(venv)   example git:(main)  wget http://127.0.0.1:8000/static/DEMO
--2022-02-10 16:02:07--  http://127.0.0.1:8000/static/DEMO
正在连接 127.0.0.1:8000... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:26 []
正在保存至: “DEMO”

DEMO                                   100%[===========================================================================>]      26  --.-KB/s  用时 0s

2022-02-10 16:02:08 (529 KB/s) - 已保存 “DEMO” [26/26])

Pure ASGI

Code

example_pure_asgi.py

Start Server

(venv)   example git:(main)  uvicorn example_pure_asgi:app

Django 3.0+

Code

/example_django/asgi.py

Collect static file

(venv)   example git:(main) cd example_django
(venv)   example_django git:(main)  python manage.py collectstatic

129 static files copied to '/Users/rex/p/asgi-middleware-static-file/example/example_django/staticfiles'.

Start Server

(venv)   example_django git:(main)  uvicorn example_django.asgi:application

Quart (Flask like)

Code

example_quart.py

Start Server

(venv)   example git:(main)  uvicorn example_quart:app

WSGI app eg: Flask, Django on WSGI mode

Code

example_wsgi_app.py

Start Server

(venv)   example git:(main)  uvicorn example_wsgi_app:app

FAQ

My static files are distributed in several different directories

You can send a list to static_root_paths; example:

static_root_paths = [ "/path/a", "path/b" ]
application = ASGIMiddlewareStaticFile(
    application,
    static_url=settings.STATIC_URL,
    static_root_paths=static_root_paths,
)

Develement

pip install -U -e ".[dev]"

History

0.7.0 - 20251218

  • chore: Drop Python 3.7/3.8/3.9 support. If you require it, please use version 0.6.2
  • feat: Add type hints and mypy support
  • feat: Add Python 3.13 compatibility for mimetypes
  • refactor: Move requirements files into pyproject.toml
  • test: 100% coverage!

0.6.2 - 20251112

  • Maintenance update

0.6.1 - 20231219

  • Maintenance update
  • Change depend policy

0.6.0 - 20230210

  • Update aiofiles to 23.1.0
  • Use more async API

0.5.0 - 20220909

  • Use more aiofiles api
  • Dropped Python 3.6 support. If you require it, use version 0.4.0
  • Update package for pep517/pep621

v0.4.0 - 20220422

  • Rewrite some code
  • Fix bug #3(Cannot serve files from root (static_url="/" becomes "//"))

v0.3.2

  • Maintenance release
  • Drop Py35

v0.3.1

  • Compatible Py37-

v0.3.0

  • Check cross border access
  • Add more type hints

v0.2.1

  • Fix bug

v0.2.0

  • Update for aiofiles
  • Fix bug

v0.1.0

  • First release

Alternative

TODO

  • zero copy
  • file extension filter,
  • Cache Control

Release files for ASGIMiddlewareStaticFile 0.7.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ASGIMiddlewareStaticFile 0.7.0
File Size Uploaded
asgimiddlewarestaticfile-0.7.0.tar.gz 8.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for ASGIMiddlewareStaticFile 0.7.0
File Interpreter ABI Platform
asgimiddlewarestaticfile-0.7.0-py3-none-any.whl Python 3 none any Details

Total release size: 15.1 kB

Release files / asgimiddlewarestaticfile-0.7.0.tar.gz

Download URL asgimiddlewarestaticfile-0.7.0.tar.gz
Size 8.0 kB
Tags Source
SHA-256 checksum
How to use checksums
70935ab3bb9cdde5cf96dcc4eed2fce39c9cf51ec371490265e673df099ddf72
BLAKE2b-256 checksum
How to use checksums
d72514ec3488ee466dd2b3c63e3cdb1f9b032d90c0c9d65ca68d25c59b74bc22
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 18, 2025.

Transparency log

Release files / asgimiddlewarestaticfile-0.7.0-py3-none-any.whl

Download URL asgimiddlewarestaticfile-0.7.0-py3-none-any.whl
Size 7.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
17796e16400d0b72062d1bfddb3892926d5620348a10bfbaff02304dedd86ca4
BLAKE2b-256 checksum
How to use checksums
182c8d5d42034d3d4180ce3e03686d3047d8261799f6d547ad69b82a035eb567
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 18, 2025.

Transparency log

Release history Release notifications | RSS feed

This release

0.7.0 This release

2 release files

0.6.2

2 release files

0.6.1

2 release files

0.6.0

2 release files

0.5.0

2 release files

0.4.0

2 release files

0.3.2

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page