You are invited to Log in or Register a free Frihost Account!

Python, CGI and CSS

Although I've looked extensively on Google, I've been unable to find an answer for Python (although there's plenty of stuff for Perl and CGI).

The question is, how do I apply an external stylesheet to the XHTML in a Python script? I tried to include the standard "<link rel='stylesheet' type='text/css' href='style1.css' />"" in with the rest of the printed XHTML. But that didn't work. When I took both files out of my cgi-bin, as a test, the CSS rules were implemented. Just so you know, both sets of permissions for these two files have been set to 755. I tried other cgi scripts (still residing in the cgi-bin) and they did work.

Could someone please help me figure this out. Thanks.
Basing from what you've said, I think that this is a path-related issue. You can probably try printing out the full path when the script is in cgi-bin. That way, you'll see the current working directory when the script runs. By the way, are you really using the script as a CGI script or are you using mod_python?
I did try "<link rel='stylesheet' type='text/css' href='http://localhost/cgi-bin/style1.css' />"", but that also did not work.

As to your second question, I'm using CGI.
And if you try this?

<link rel='stylesheet' type='text/css' href='/style1.css' />

That should work (assuming style1.css is at the root of your documentroot)
I just tried that, but it also didn't work. If I give you the files, could you see if I did anything else wrong?


#       This is a test CGI script written in Python

import cgi
import cgitb; cgitb.enable()

def main():
   print "Content-type: text/html\n"
   print "<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Strict//EN'"
   print "    ''>"
   print "<html xmlns=''>"
   print "    <head>"
   print "        <title>Hello World!</title>"
   print "        <meta http-equiv='Content-Type'"
   print "        content='text/html; charset=utf-8' />"
   print "        <link rel='stylesheet' type='text/css' href='/style1.css' />"
   print "    </head>"
   print "    <body>"
   print "        <p>Hello <br />"
   print "        World! <br />"
   print "        </p>"
   print "        <p>"
   for i in range(1, 4):
      for j in range(1, 13):
         print " "*7, "%d X %d = %d" % (i, j, i*j), "<br />"
      print " "*7, "<br />"
   print "        </p>"
   print "    </body>"
   print "</html>"

if __name__ == "__main__":


color: blue;

It wouldn't be that I need to edit my httpd.conf file so that Apache can use CSS files right?

I doubt the css file is in the root.
And nothing needs to be done on the serverside to make it work with css files.
try to just put the html in an html file and open that, if that works you know at least the html is right.
Well, I had taken both files out of the cgi-bin and it worked. So I think that the code itself is correct.

Ok, I checked the error log. This is what I'm getting: (2)No such file or directory:

When I get home I'll put the CSS file in my Mac's Sites folder (which I have set as the DocumentRoot folder).
Thank you all for your help. I've finally figured it out. I had typed in the path for the CSS file into my browser, which resulted in an internal service error. When I checked the error log, I had received the following:

[Thu Apr 10 20:38:47 2008] [error] [client ::1] (8)Exec format error: exec of '/usr/local/apache2/cgi-bin/style1.css' failed

If I understand this correctly, Apache was apparently trying to execute my CSS file. So I moved the CSS file into my /~username/Sites folder (I'm on a Mac), changed the href link in my Python script, and finally my CSS rules were applied to the selected XHTML. I read somewhere that it's not a good idea to have anything other than scripts in one's cgi-bin anyways. Thanks again!
Related topics
Does frihost still support Python for CGI programming
Python beginners help!
Python CGI work with FRIHOST
Problem with python cgi - does Python CGI work?
Python support for MySQL
Python via CGI - A personal triumph
Python via CGI - A personal triumph
How to debug Python CGI errors
Python Support ???
Is there a Python Hello World using Apache?
I find python is still not as popular as perl
I find python is still not as popular as perl
Python CGI script 500 error
Tutorial needed for python cgi
Reply to topic    Frihost Forum Index -> Scripting -> Others

© 2005-2011 Frihost, forums powered by phpBB.