Roberto,
Thanks for the reply. I agree, I should be serving static files (*.js,
*.css, etc.) from the webserver, I guess I don't know how to do that;
get Apache2 (what we're using) to serve up the files from the /public
folder inside my Pylons project. Got any pointers, or does this fall
under the "Web Serving 101"?
The rest of this response is the contents of the gzip middleware I
mentioned. This is taken almost verbatim from "The Definitive Guide to
Pylons" book.
import gzip
import StringIO
class GzipMiddleware(object):
'''This class implements gzip compression for *.js and *.css files
delivered by the pylons applications'''
def __init__(self, app, compresslevel=9):
self.app = app
self.compresslevel = compresslevel
def __call__(self, environ, start_response):
if 'gzip' not in environ.get("HTTP_ACCEPT_ENCODING", ""):
return self.app(environ, start_response)
if environ["PATH_INFO"][-3:] != ".js" and environ["PATH_INFO"]
[-4:] != ".css":
return self.app(environ, start_response)
buffer = StringIO.StringIO()
output = gzip.GzipFile(
mode="wb",
compresslevel=self.compresslevel,
fileobj=buffer
)
start_response_args = []
def dummy_start_response(status, headers, exc_info=None):
start_response_args.append(status)
start_response_args.append(headers)
start_response_args.append(exc_info)
return output.write
app_iter = self.app(environ, dummy_start_response)
for line in app_iter:
output.write(line)
if hasattr(app_iter, "close"):
app_iter.close()
output.close()
buffer.seek(0)
result = buffer.getvalue()
headers = []
for name, value in start_response_args[1]:
if name.lower() != "content-length":
headers.append((name, value))
headers.append(("Content-Length", str(len(result))))
headers.append(("Content-Encoding", "gzip"))
start_response(start_response_args[0], headers,
start_response_args[2])
buffer.close()
return [result]
On Sep 20, 10:18 am, Roberto De Ioris <[email protected]> wrote:
> Il giorno 20/set/2010, alle ore 16.02, writeson ha scritto:
>
>
>
>
>
> > Hi all,
> > I've been building some Pylons web applications for our Intranet.
> > These are deployed using a standalone uwsgi server (http://
> > projects.unbit.it/uwsgi), which seems to work well, except for one
> > issue. I've incorporated the GzipMiddleware.py code shown in the
> > Pylons book into my config/middleware.py code. This works great when
> > run with paster, but when my application is deployed under uwsgi the
> > files that are supposed to get gzipped (*.css, *.js) are reporting
> > back:
>
> > NS_ERROR_INVALID_CONTENT_ENCODING
>
> > in HttpFox. Does anyone have any ideas, suggestions or pointers that
> > might help me out? My applications use quite a few jQuery plugin files
> > as well as jQuery UI css files, so I'd like to compress these if I
> > can.
>
> Why are you serving those files via uWSGI ?
>
> You should serve static files via the webserver, it is the right tool for the
> job.
>
> (obviously if they are dinamically generated ignore my response)
>
> By the way, could you post this "GzipMiddleware.py" so i can check it ?
>
> Thanks
>
> --
> Roberto De Iorishttp://unbit.it
> JID: [email protected]
--
You received this message because you are subscribed to the Google Groups
"pylons-discuss" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/pylons-discuss?hl=en.