ASGI Middleware for serving Static File.
Project description
ASGI Middleware for serving Static File.
Install
pip3 install -U ASGIMiddlewareStaticFile
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
- ASGI Middleware
django.contrib.staticfiles.handlers.ASGIStaticFilesHandler
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
Close
Hashes for ASGIMiddlewareStaticFile-0.2.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | bf2b1cbe862da00ddda1a87ff10104692314248fd8355ea68da447ba30f487ab |
|
MD5 | 3fa857813eda2fc6da591a980ba47d5c |
|
BLAKE2b-256 | f38cd370a659a395062693d14528974ec15bb6ae7f7d30f264b10884bb3170ae |
Close
Hashes for ASGIMiddlewareStaticFile-0.2.0-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0a7c3d463d5a40bb42ee0786784c214166942b9f396c9ac0dc862b95ca76a7d7 |
|
MD5 | bce5f19856f891378ec3a64fa2ce1ed7 |
|
BLAKE2b-256 | ce2d9ac6bdb1ac234456ea6c18940c7b8f0a4b8c7ebc2e0d58b3ade30877934f |