wsgi with separate css file

Rami Chowdhury rami.chowdhury at gmail.com
Fri Nov 13 18:16:27 CET 2009


On Fri, 13 Nov 2009 08:55:33 -0800, Alena Bacova <athenka25 at gmail.com>  
wrote:

> Hi,
> I'm using:
>
> from wsgiref import simple_server
> httpd = simple_server.make_server(HOST, PORT, Test)
> try:
>     httpd.serve_forever()
> except KeyboardInterrupt:
>       pass
>
> But I can use something else if needed.  Application and htmk, css and
> images are stored on the same machine, everything is stored in one  
> folder.
>
> Alena.
>

If you are just using this to learn to develop WSGI applications, I would  
recommend implementing a method to distinguish requests for static files,  
and handle them separately. A very naive implementation handling .css  
files might look like:

	from wsgiref import util
	import os

	def do_get(environ, start_response):
		REQUEST_URI = util.request_uri(environ)
		if (REQUEST_URI.endswith('.css')):
			return do_css(REQUEST_URI, start_response)
		
		# your regular code here

	def do_css(request_uri, start_response):
		full_path = os.path.abspath(os.path.join(MY_HTTP_DIRECTORY, request_uri))
		if os.path.exists(full_path):
			file_obj = open(full_path, 'r')
			response_lines = file_obj.readlines()
			file_obj.close()
			start_response('200 OK', [('Content-Type', 'text/css')])
			return response_lines
		else:
			start_response('404 Not Found', [])
			return []

However, this method is fragile and very inefficient. If you want to  
eventually deploy this application somewhere, I would suggest starting  
with a different method.

Hope that helps,
Rami

-- 
Rami Chowdhury
"Never attribute to malice that which can be attributed to stupidity" --  
Hanlon's Razor
408-597-7068 (US) / 07875-841-046 (UK) / 0189-245544 (BD)



More information about the Python-list mailing list