Skip to main content

ASGI Middleware for serving Static File.

Project description

https://img.shields.io/pypi/v/ASGIMiddlewareStaticFile.svg https://img.shields.io/pypi/pyversions/ASGIMiddlewareStaticFile.svg https://img.shields.io/pypi/dm/ASGIMiddlewareStaticFile.svg

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ASGIMiddlewareStaticFile-0.2.1.tar.gz (5.4 kB view details)

Uploaded Source

Built Distribution

ASGIMiddlewareStaticFile-0.2.1-py2.py3-none-any.whl (5.1 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file ASGIMiddlewareStaticFile-0.2.1.tar.gz.

File metadata

  • Download URL: ASGIMiddlewareStaticFile-0.2.1.tar.gz
  • Upload date:
  • Size: 5.4 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

Hashes for ASGIMiddlewareStaticFile-0.2.1.tar.gz
Algorithm Hash digest
SHA256 691ed751e3b8ee8d8eb04ee99df80325a1b6cc621b9aceccb59d26e88d2810a8
MD5 84ee66343a7c0660522c83a04f453dec
BLAKE2b-256 8198f6ee6d156713581f6ca91bc8eaf69e3b0ce743159653df49310de6f345ea

See more details on using hashes here.

File details

Details for the file ASGIMiddlewareStaticFile-0.2.1-py2.py3-none-any.whl.

File metadata

  • Download URL: ASGIMiddlewareStaticFile-0.2.1-py2.py3-none-any.whl
  • Upload date:
  • Size: 5.1 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

Hashes for ASGIMiddlewareStaticFile-0.2.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 d378a473d94f6ea82d25f593a838ec290dc2094f17c3229c49f715c61dda6fdf
MD5 dc54113e1162de1e2b41f6c0bf0b3f94
BLAKE2b-256 81c4d91aab511c041a5eb5456a9d9a4ba1eb98327343c400b2bce50a53af4956

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page