0

How to serve static files like css with a WSGI application written using werkzeug and pure

python.. No frameworks used..

this is my server..

    from gevent import pywsgi
from Index import application

import os
application = SharedDataMiddleware(application, {
    '/hello/template/': os.path.join(os.getcwd(), 'template')
})
print 'Serving on https://127.0.0.1:4000'
server = pywsgi.WSGIServer(('0.0.0.0', 4000), application,spawn=10000)

# to start the server asynchronously, call server.start()
# we use blocking serve_forever() here because we have no other jobs
server.serve_forever()

The template is the the path to static files like css and images. but this is only serving the application and not the static files. is there a feature where in gevent serves static files .. ? I did not find the documentation useful.

rakesh
  • 975
  • 2
  • 11
  • 15

1 Answers1

1

Serving static files directly from your WSGI application is a waste of resources, both CPU and memory, and it will not scale.

For public static files you should configure your front-end webserver to serve them directly.

For private static files you can do access control and construct a response header in your WSGI app, and then let the front-end webserver do the heavy lifting of actually sending the file contents. Take a look at X-Sendfile (Apache) or X-Accel-Redirect (Nginx).

Daniel Eriksson
  • 3,814
  • 3
  • 16
  • 11