ASGI Middleware for serving Static File.
Project description
ASGI Middleware for serving Static File.
Usage
Pure ASGI
import os
from wsgiref.simple_server import make_server
from asgi_middleware_static_file import ASGIMiddlewareStaticFile
BASE_DIR = os.path.dirname(__name__)
STATIC_DIRS = [os.path.join(BASE_DIR, 'static')]
def app(env, start_response):
start_response('200 OK', [('Conte-type', 'text/plain; charset=utf-8')])
return [b'Hello World']
app = ASGIMiddlewareStaticFile(app, static_url='static', static_paths=STATIC_DIRS)
if __name__ == '__main__':
httpd = make_server('', 8000, app)
httpd.serve_forever()
Django
Update asgi.py
import os
from django.conf import settings
from django.core.asgi import get_asgi_application
from asgi_middleware_static_file import ASGIMiddlewareStaticFile
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'dj_project.settings')
application = get_asgi_application()
application = ASGIMiddlewareStaticFile(
application, static_url=settings.STATIC_URL, static_paths=[settings.STATIC_ROOT]
)
Do’t forget execute python manage.py collectstatic
Run server
daphne dj_project.asgi:application -b 0.0.0.0
2020-04-14 17:20:57,530 INFO Starting server at tcp:port=8000:interface=0.0.0.0
2020-04-14 17:20:57,531 INFO HTTP/2 support not enabled (install the http2 and tls Twisted extras)
2020-04-14 17:20:57,531 INFO Configuring endpoint tcp:port=8000:interface=0.0.0.0
2020-04-14 17:20:57,532 INFO Listening on TCP address 0.0.0.0:8000
127.0.0.1:62601 - - [14/Apr/2020:17:21:08] "GET /static/css/emo.css" 200 1692
Alternative
django.contrib.staticfiles.handlers.ASGIStaticFilesHandler
https://github.com/kobinpy/wsgi-static-middleware only work with WSGI
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file ASGIMiddlewareStaticFile-0.1.0.tar.gz
.
File metadata
- Download URL: ASGIMiddlewareStaticFile-0.1.0.tar.gz
- Upload date:
- Size: 4.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.7.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d6e42a3c8d3efdb7e18c1fe52b9b4e7511d1c0d48240fa7d30af462f7874871b |
|
MD5 | 1e3a3889a71cb548b6a9b2d935327ef7 |
|
BLAKE2b-256 | 6e87482992308ef50e359d9712545259e9edefc78b24fd464953d1eef19397d8 |
File details
Details for the file ASGIMiddlewareStaticFile-0.1.0-py2.py3-none-any.whl
.
File metadata
- Download URL: ASGIMiddlewareStaticFile-0.1.0-py2.py3-none-any.whl
- Upload date:
- Size: 4.8 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.7.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a300932a89b196b6838f5f21cf653a6bae92538c3e5edd0830267bfb4bd0e21b |
|
MD5 | 211e68a8e01c1d354252e29133f4de22 |
|
BLAKE2b-256 | dbd530ba89e642a91043655348aed5ff1eabd9d7a3e51e0d94f22d92760a760e |