Python CGI script and CSS style sheet
Tim Chase
python.list at tim.thechases.com
Wed Jan 23 11:15:15 EST 2008
> I'm working with a Python CGI script that I am trying to use with an
> external CSS (Cascading Style Sheet) and it is not reading it from the
> web server. The script runs fine minus the CSS formatting. Does
> anyone know if this will work within a Python CGI? It seems that line
> 18 is not being read properly. One more thing. I tested this style
> sheet with pure html code (no python script) and everything works
> great.
>
> Listed below is a modified example.
>
> ++++++++++
>
> 1 #!/usr/bin/python
> 2
> 3 import cgi
> 4
> 5 print "Content-type: text/html\n"
The answer is "it depends". Mostly on the configuration of your
web-server. Assuming you're serving out of a cgi-bin/ directory,
you'd be referencing
http://example.com/cgi-bin/foo.py
If your webserver (apache, lighttpd, whatever) has been
configured for this directory to return contents of
non-executable items, your above code will reference
http://example.com/cgi-bin/central.css
and so you may be able to just drop the CSS file in that directory.
However, I'm fairly certain that Apache can be (and often is)
configured to mark folders like this as "execute only, no
file-reading". If so, you'll likely get some sort of Denied
message back if you fetch the CSS file via HTTP.
A better way might be to reference the CSS file as
"/media/central.css"
12 <link rel="stylesheet" type="text/css"
href="/media/central.css" />
and then put it in a media folder which doesn't have the
execute-only/no-read permission set.
Another (less attractive) alternative is to have your CGI sniff
the incoming request, so you can have both
http://example.com/cgi-bin/foo.py
http://example.com/cgi-bin/foo.py?file=css
using the 'file' GET parameter to return the CSS file instead of
your content. I'd consider this ugly unless deploy-anywhere is
needed, in which case it's not so bad because the deployment is
just the one .py file (and optionally an external CSS file that
it reads and dumps).
-tkc
More information about the Python-list
mailing list